Asp.net mvc 3 将所选下拉选项的id传递给控制器
我正在使用mvc3 nhibernate并创建一个搜索应用程序。。。 在这里,我创建了一个包含所有爱好名称的下拉列表,点击搜索按钮,所选选项的id将转到post方法 我在控制器中编写了以下代码Asp.net mvc 3 将所选下拉选项的id传递给控制器,asp.net-mvc-3,nhibernate,html.dropdownlistfor,Asp.net Mvc 3,Nhibernate,Html.dropdownlistfor,我正在使用mvc3 nhibernate并创建一个搜索应用程序。。。 在这里,我创建了一个包含所有爱好名称的下拉列表,点击搜索按钮,所选选项的id将转到post方法 我在控制器中编写了以下代码 public ActionResult Details() { ViewBag.h=new SelectList(new Hobby_MasterService().GetHobbies(),"Hobby_Id"); return View(); } [HttpPost] public ActionR
public ActionResult Details()
{
ViewBag.h=new SelectList(new Hobby_MasterService().GetHobbies(),"Hobby_Id");
return View();
}
[HttpPost]
public ActionResult Details(int Hobby_Id)
{
Hobby_Master hm = new Hobby_MasterService().GetHobby_Data(Hobby_Id);
return RedirectToAction("Show");
}
在视图中,我只显示了一个下拉列表
<b>Select Hobby:</b>
@using (Html.BeginForm("Details", "Hobbies", FormMethod.Get))
{
<div class="Editor-field">
@Html.DropDownListFor(Model => Model.Hobby_Id, (IEnumerable<SelectListItem>)ViewBag.h)
</div>
<input type="submit" value="Search" />
}
选择爱好:
@使用(Html.BeginForm(“细节”、“爱好”、FormMethod.Get))
{
@Html.DropDownListFor(Model=>Model.Hobby_Id,(IEnumerable)ViewBag.h)
}
我的下拉列表是通过一个函数填充的,该函数有一个普通的sql语句。。。
我可以生成列表…但是我如何获得选定的爱好id。。。
请帮助也许是FormMethod。是否在您的表单上发布 你的模特是一流的吗?
也许您可以接受这一点,在post操作中,您将在其上找到id。不必担心模型绑定,您只需将FormCollection参数添加到post方法中即可。该集合包含所有已发布的表单值
[HttpPost]
public ActionResult Details(FormCollection collection)
{
Hobby_Master hm = new Hobby_MasterService().GetHobby_Data(Hobby_Id);
if (collection["Hobby_Id"] != null)
{
// collection["Hobby_Id"] contains the value selected in the dropdown box
}
return RedirectToAction("Show");
}
“不工作”是什么意思?我将启动调试器并查看在集合参数中将哪些值发布到方法。键应该是“Hobby_Id”,对应于您发布的视图代码,但我会查看调试器以确保。