我试图从ASP .NET MVC 5(C#)中的@ Html.DropDownList 获取SELECTED值。我在View中获得了这个DropDownList:
@Html.DropDownList("CustomCarsList", ViewBag.CustomCars as SelectList)
ViewBag.CustomCars
在公共ActionResult中的 UnregisteredSectionController 中定义
UnregisteredSection()
方法为:
string selectString = "Select * from CustomCars where OwnerId ='" + User.Identity.GetUserId() +"'";
ViewBag.CustomCars = new SelectList(db.Garage.SqlQuery(selectString), "CustomCarID", "CustomCarDescription", selectedInt);
我不想使用JavaScript,我尝试了很多教程,但没有成功。必须将选定的值作为其他已创建函数的参数传递。
修改
这是UnregisteredSection视图中的DrowDownList代码:
@using (Html.BeginForm(FormMethod.Post)) {
@Html.DropDownList("selectedCustomCar", (SelectList)ViewBag.CustomCars);
<button type="submit">Submit</button>
}
这是UnregisteredSectionController中的代码:
public ActionResult UnregisteredSection(string dropdownlistReturnValue)
{
string selectString = "Select * from CustomCars where OwnerId ='" + User.Identity.GetUserId() +"'";
//To pass list of Departments from the controller, store them in ViewBag
ViewBag.CustomCars = new SelectList(db.Garage.SqlQuery(selectString), "CustomCarID", "CustomCarDescription", selectedInt);
//toTest
System.Diagnostics.Debug.WriteLine(dropdownlistReturnValue);
return View();
}
最后这是型号代码:
public class CustomCar
{
public int CustomCarID { get; set; }
public string Name { get; set; }
public int TankCapacity { get; set; }
public string FuelType { get; set; }
public int Maxoutput { get; set; }
public decimal FuelOutside { get; set; }
public decimal FuelTown { get; set; }
public decimal FuelMix { get; set; }
public string OwnerId { get; set; }
public int selectedCustomCar { get; set; } //new
public string CustomCarDescription
{
get
{
return string.Format("{0}, {1}kW, {2}", Name, Maxoutput, FuelType);
}
}
}
public class DefaultConnection : DbContext
{
public DbSet<CustomCar> Garage { get; set; }
}
答案 0 :(得分:2)
使用绑定到模型的强类型帮助程序。如果您的模型包含属性CustomCarsList
public class CustomCar
{
....
public int CustomCarsList { get; set; } // assumes `CustomCarID` is typeof int
}
然后在视图中
@model CustomCar
@using (Html.BeginForm()) {
....
@Html.DropDownListFor(m => m.CustomCarsList, (SelectList)ViewBag.CustomCars)
<button type="submit">Submit</button>
}
并在控制器中
public ActionResult UnregisteredSection(string dropdownlistReturnValue) // dont know what dropdownlistReturnValue is doing?
{
CustomCar model = new CustomCar();
model.CustomCarsList = // some value
ViewBag.CustomCars = new SelectList(db.Garage.SqlQuery(selectString), "CustomCarID", "CustomCarDescription"); // selectedInt is not required
return View(model);
}
[HttpPost]
public ActionResult UnregisteredSection(CustomCar model)
{
// model.CustomCarsList contains the value of the selected option
}
然后,如果CustomCarsList
的值与您的某个选项值(由CustomCarID
定义)的值匹配,那么这是将被选中的选项,否则将选择第一个选项。在回发后,CustomCarsList
的值将是用户选择的选项的值。
答案 1 :(得分:0)
检查select元素以获取select标记的名称。然后在post actionResult
的参数中使用它[HttpPost]
public ActionResult YourServerMethod(string selectname)
{
}
Asp会为你做其余的事情
答案 2 :(得分:0)
为了使自动绑定起作用,selectedCustomCar
(选择输入的名称)和dropdownlistReturnValue
(控制器操作参数名称)应该具有相同的名称(这是按照惯例)。
因此,您应该将选择名称更改为dropdownlistReturnValue
:
@Html.DropDownList("dropdownlistReturnValue", ViewBag.CustomCars as SelectList)
或更改参数名称:
public ActionResult UnregisteredSection(string selectedCustomCar)
回发后,您的参数将包含下拉选择列表中的选定值。
答案 3 :(得分:0)
在遭遇同样问题两天之后,我终于得到了答案。
http://silverlightpractice.blogspot.com/2014/03/how-to-get-htmldropdownlistfor-selected.html
我建议,
不要将模型属性保持为&#34;必需&#34;从视图模型中,以便您的模型状态始终有效。当您在控制器上收到操作时,请手动填写属性。
在您的控制器中,您只需调用以下方法即可从下拉列表中找到所选值。
string sCar = Request.Form [&#34; CustomCarsList&#34;];
希望它可以帮助别人!