1

我的视图包含下拉列表。填充下拉列表代码:

public ActionResult Create()
{
  var architects = (from ar in db.ArchitectMasters where ar.IsActive == 1 select ar).ToList();
  ProjectMaster ps = new ProjectMaster();
  ViewBag.ArchitectID = new SelectList(architects, "ArchitectID", "ArchitectName",ps.ArchitectID);
  return View();
}

查看代码:

     <div class="editor-label">
        @Html.Label("Architect")
    </div>
    <div class="editor-field">
         @Html.Hidden("ArchitectID")
         @Html.DropDownList("ArchitectID ", (IEnumerable<SelectListItem>)ViewBag.ArchitectID)
    </div>

后期创建方法:

    public ActionResult Create(ProjectMaster projectmaster, long ArchitectID, FormCollection data)
    {
         if (ModelState.IsValid)
        {
            //string dropDownSelectedValue = data.Get("ArchitectName");
            //ViewBag.ArchitectID = data["ArchitectName"];
            long architectname = long.Parse(ArchitectID.ToString());
             db.ProjectMasters.Add(projectmaster);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
    }

如何在我的控制器中获取下拉列表的选定值?请帮忙。

4

1 回答 1

0

我猜您可以从 Model/FormCollection 中检索 ActionResult 中的选定值。通常遵循以下方式来获取控制器中下拉列表的选定值。

public ActionResult Create(ProjectMaster projectmaster, long ArchitectID, FormCollection data)
    {
         if (ModelState.IsValid)
        {
            int id=projectmaster.ArchitectID;
           //another way int id=data["ArchitectID"];  

            return RedirectToAction("Index");
        }
    }
于 2013-03-14T10:02:59.760 回答