Selecting a value from a DropDownList to be able t

2019-09-13 08:11发布

问题:

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();
}