0

我正在尝试将选定的值与模型中定义的 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();
    }
4

1 回答 1

0

您说您在发布此表单时收到此错误。也许你没有SelectListView你的[HttpPost]版本中传递Action?

编辑:

作为对您的编辑的回应:您还应该在版本中包含SelectList代码。[HttpPost]

于 2013-05-29T15:03:19.620 回答