Selecting a value from a DropDownList to be able to fill in fields

纵饮孤独 提交于 2019-12-25 06:39:35

问题


I have populated a DropDownList with the following: Model:

namespace VAGTC.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Organization
    {

        public int OrganizationID { get; set; }
        public string Name { get; set; }

}

Controller:

public ActionResult Index()
{

    ViewBag.DropOrgID = ddp.OrganizationList();
    return View();
}

ViewModel:

namespace VAGTC.ViewModels
{
    public class OrgDrop
    {
        public Organization organization { get; set; }
        public IEnumerable<Organization> Organizations {get; set;}
    }
}

Helper:

    public class DropDownPopulatorController : Controller
    {
        private VAGTCEntities db = new VAGTCEntities();

        public SelectList OrganizationList(int selectedOrg = 0)
        {
            var orgQuery = from d in db.Organizations
                           orderby d.Name
                           select d;
            return new SelectList(orgQuery, "OrganizationID", "Name", selectedOrg);
        }
    }

Then, I simply put "@Html.DropDownList("DropOrgID")" in my index view.

What I have to accomplish is filling in text boxes (which I have not yet implemented) by fetching data from my database depending on what is selected in the DropDownList. But I could not figure this out - I feel as if I went about creating and populating the drop down incorrectly.

I am new to MVC and I am slowly learning! I am taking it step-by-step! Focusing now on just fetching a SelectedValue. I have tried many ways of creating and populating a drop down but this is the one that worked for me. I looked up tutorials for fetching the SelectedValue but it doesn't seem to work with the way I made the drop down. Specifcally, I found the Cascading DropDownList but I could not get it to work with my drop down.

So my question: How does one go about fetching a SelectedValue and then pass the value along to a controller and then a model (I think that would be how it works?) to select data based on the selection.

Any help is extremely appreciated.

For reference: here is my drop down

Thank you!


回答1:


You can Get Drop-down's selected value in Post Action in Controller as,

[HttpPost]
public ActionResult Index(FormCollection frm)
{
   var selectedValue = frm["DropOrgID"];
   return View();
}


来源:https://stackoverflow.com/questions/13301235/selecting-a-value-from-a-dropdownlist-to-be-able-to-fill-in-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!