我正在尝试将选定的值与模型中定义的 aa 参数相关联,但现在出现错误:“值不能为空。参数名称:项目”
在模型中我有:
public class ObjectivesModel
{
[Required(ErrorMessage = "*")]
[Display(Name = "AssociatedGoal")]
public int AssociatedGoal { get; set; }
}
在 Controler 中,我创建了列表
[HttpGet]
public ActionResult NewObjective()
{
IEnumerable<SelectListItem> example = new List<SelectListItem>
{
new SelectListItem() { Text = "ola1", Value = "1" },
new SelectListItem() { Text = "ola2", Value = "2" }
};
ViewBag.GoalsToAssociate = example;
return View();
}
最后在视图中我有
@using (Html.BeginForm()){
<div class="editor-label">@Html.LabelFor(o => o.AssociatedGoal)</div>
<div class="editor-DropDownList">@Html.DropDownListFor(o => o.AssociatedGoal, new SelectList(@ViewBag.GoalsToAssociate, "Value", "Text"), "--Select Goal--")
@Html.ValidationMessageFor(o => o.AssociatedGoal)
</div>
所以当我提交表单时会发生错误。我在控制器的发布版本上有任何特殊代码
[HttpPost]
public ActionResult NewObjective(Models.ObjectivesModel objective)
{
return View();
}