Chris
Chris

Reputation: 1642

MVC3 DropDownListFor Model is null on PostBack

I am using a DropDownListFor like this:

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

And I am also using:

[Required]

As Attribute in the View.

So when I PostBack the View to the Server and the Required Attribute will fail, the View is showed again, but then Model is null. So I will get a NullReferenceException in Model.OrganisationList.

Upvotes: 0

Views: 1371

Answers (1)

Shyju
Shyju

Reputation: 218702

That is the default behaviour. As you know ,MVC does not rely on ViewState, It can not keep the content inside the drop down across Postbacks (generic term). You need to repopulate it again in the ActionMethod.

Upvotes: 2

Related Questions