Not sure how to get dropdown values from view model
I'm not sure how to get ALL of my values from the view. I am using a strongly typed view model called ApplicationViewModel, here is the code:
public Application Application { get; private set; }
public SelectList AccountTypes { get; private set; }
public ApplicationViewModel(Application application, IEnumerable<AccountType> accountTypes)
{
Application = application;
AccountTypes = new SelectList(accountTypes, "AccountTypeID", "AccountTypeName", application.AccountTypeID);
}
When the form loads for the first time the dropdown populates correctly, and it validates correctly. Here is my dropdown code:
<%: Html.DropDownListFor(m => m.Application.AccountType, Model.AccountTypes, "-- Select --")%>
<%: Html.ValidationMessageFor(m => m.Application.AccountType) %>
When I click the submit button then I want to get all the values on the form, including the value selected in the dropdown. I'm not sure what I am doing wrong. In my Create action it is setting the properties of the Application class to the values of the textboxes. But it is not setting AccountType / AccountTypeID. How do I set this? Is my Create Acction incorrect? Here is my Create action:
[HttpPost]
public ActionResult Create(Application application)
{
开发者_Go百科}
Please could someone advise?
Thanks.
This is probably due to a naming mismatch between the view and the model the binder is trying to populate. Since (I assume) the DropDownList returns the selected Account Type ID, that needs to match with the property in your model.
I would guess you need something like:
<%: Html.DropDownListFor(m => m.Application.AccountTypeId, Model.AccountTypes, "-- Select --")%>
<%: Html.ValidationMessageFor(m => m.Application.AccountTypeId) %>
otherwise, degbug the response form values and see what name the selected value is being saved as.
精彩评论