[英]how do i select value from dropdownlist to mode or to controller mvc
Model 模型
public class Company
{
public int ID { get; set; }
....
public int WorkZoneID { get; set; }
public IEnumerable<SelectListItem> WorkZones { get; set; }
Controller: 控制器:
ViewBag.Workzones = cmpRep.GetWorkzoneDrop();
Repository: 仓库:
public List<SelectListItem> GetWorkzoneDrop()
{
SqlDataReader DR;
DR = Ado.ExecDataReaderProc("WorkZoneSelectActive");
List<SelectListItem> selectWorkZoneListItems = new List<SelectListItem>();
CompanyVMList C = new CompanyVMList();
C.Companies = new List<Company>();
while (DR.Read())
{
SelectListItem selectListItem = new SelectListItem
{
Text = Convert.ToString(DR["Name"]),
Value = Convert.ToString(DR["ID"]),
};
selectWorkZoneListItems.Add(selectListItem);
}
return selectWorkZoneListItems;
}
View 视图
@Html.DropDownList("Workzones")
I want to assign selected dropdownlist value to int work zone int the mode how do i select value from dropdownlist to model or to controller 我想将选定的dropdownlist值分配给int工作区int模式我如何从dropdownlist中选择值以建模或分配给控制器
You need to bind your dropdownlist to property WorkZoneID
您需要将下拉列表绑定到属性
WorkZoneID
@Html.DropDownListFor(m => m.WorkZoneID, Model.Workzones)
and if your wanting to preselect an option, you need to set the value in the controller before you pass the model to the view. 如果要预选一个选项,则需要在控制器中设置值,然后再将模型传递给视图。 And since your model contains a property for the
SelectList
, then use it (don't use ViewBag
) 并且由于您的模型包含
SelectList
的属性,请使用它(不要使用ViewBag
)
Company model = new Company
{
WorkZoneID = 1 // set to a value that matches one of your options
Workzones = cmpRep.GetWorkzoneDrop();
};
return View(model);
and when you submit to 当你提交给
[HttpPost]
public ActionResult Edit(Company model)
the value of model.WorkZoneID
will contain the value of the selected option. model.WorkZoneID
的值将包含所选选项的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.