从DropDownList中选择一个值以填充字段

时间:2012-11-09 02:21:54

标签: asp.net-mvc-3 selectedvalue html-select

我已使用以下内容填充DropDownList: 型号:

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

    public partial class Organization
    {

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

}

控制器:

public ActionResult Index()
{

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

视图模型:

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

助手:

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

然后,我简单地把&#34; @ Html.DropDownList(&#34; DropOrgID&#34;)&#34;在我的索引视图中。

我必须完成的工作是根据DropDownList中选择的内容从数据库中提取数据来填写文本框(我尚未实现)。但我无法理解这一点 - 我觉得好像我开始创建和填充下拉不正确。

我是MVC的新手,我正在慢慢学习!我正在逐步采取它!现在关注仅提取SelectedValue。我已经尝试了许多创建和填充下拉菜单的方法,但这是适合我的方法。我查找了获取SelectedValue的教程,但它似乎与我下拉的方式一起工作。具体来说,我找到了Cascading DropDownList,但我无法让它与我的下拉工作。

所以我的问题是:如何获取SelectedValue,然后将值传递给控制器​​,然后将模型(我认为它将如何工作?)根据选择选择数据。

非常感谢任何帮助。

供参考:here is my drop down

谢谢!

1 个答案:

答案 0 :(得分:-1)

您可以在Controller中的Post Action中获取下拉列表中的选定值,

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