This is the sample of the model class
这是model类的示例
[Required(ErrorMessage = "Department is required")]
[Display(Name = "Department")]
public int SelectedValue { get; set; }
[Required(ErrorMessage = "Department is required")]
[Display(Name = "Department")]
public List<Department> RoleList { get; set; }
This is the view
这是
<div class="col-md-10">
@Html.DropDownListFor(m => m.RoleList, new SelectList(Model.RoleList,"ID", "RoleName","1"), "Please Choose a Department", new { htmlAttributes = new { @class = "form-control"} })
@Html.ValidationMessageFor(model => model.SelectedValue, "",new { @class = "text-danger"})
</div>
The list is populated perfectly from the database. Now when i select the item on the list, the select value has to be sent to the controller
该列表从数据库中完美地填充。现在,当我在列表中选择项目时,必须将选择值发送给控制器。
public async Task<ActionResult> Register(RegisterViewModel model)
{
int Selectedvalue = model.SelectedValue;
if (ModelState.IsValid && (model.SelectedValue).ToString() != "0")
{
Here i have made a selection
这里我做了一个选择
Error
错误
1 个解决方案
#1
3
You should change it
你应该改变它
@Html.DropDownListFor(m => m.RoleList, new SelectList(Model.RoleList,"ID", "RoleName","1"), "Please Choose a Department", new { htmlAttributes = new { @class = "form-control"} })
To
来
@Html.DropDownListFor(m => m.SelectedValue, new SelectList(Model.RoleList,"ID", "RoleName","1"), "Please Choose a Department", new { htmlAttributes = new { @class = "form-control"} })
The first parameter of the DropDownListFor
should be selected value in model. In your case, this property is Model.SelectedValue
not Model.RoleList
.
DropDownListFor的第一个参数应该在模型中选择值。在您的例子中,这个属性是Model。SelectedValue Model.RoleList。
#1
3
You should change it
你应该改变它
@Html.DropDownListFor(m => m.RoleList, new SelectList(Model.RoleList,"ID", "RoleName","1"), "Please Choose a Department", new { htmlAttributes = new { @class = "form-control"} })
To
来
@Html.DropDownListFor(m => m.SelectedValue, new SelectList(Model.RoleList,"ID", "RoleName","1"), "Please Choose a Department", new { htmlAttributes = new { @class = "form-control"} })
The first parameter of the DropDownListFor
should be selected value in model. In your case, this property is Model.SelectedValue
not Model.RoleList
.
DropDownListFor的第一个参数应该在模型中选择值。在您的例子中,这个属性是Model。SelectedValue Model.RoleList。