PostBack上的MVC3 DropDownListFor Model为null

时间:2012-03-06 00:14:13

标签: asp.net-mvc-3 model postback nullreferenceexception html.dropdownlistfor

我正在使用DropDownListFor:

@Html.DropDownListFor(model => model.SelectedOrganisationValue, new SelectList(Model.OrganisationList, "Value", "Text"))

我也在使用:

[Required]

视图中的属性。

因此,当我将ViewBack返回到服务器并且Required Attribute将失败时,将再次显示View,但是Model为null。所以我将在Model.OrganisationList中获得NullReferenceException。

1 个答案:

答案 0 :(得分:2)

这是默认行为。如您所知,MVC不依赖于ViewState,它无法将内容保留在Postbacks(通用术语)的下拉列表中。您需要在ActionMethod中重新填充它。