I’m not sure what I am doing wrong here, not even sure if I am on the right track. I have a view model and I create a drop down list from it. Here is my view model:
public class ApplicationViewModel
{
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);
}
}
Here is my Create (get) action:
public ActionResult Create()
{
var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);
return View(viewModel);
}
And my view code:
<%: Html.DropDownListFor(???, Model.AccountTypes, "-- Select --") %>
<%: Html.ValidationMessageFor(???) %>
In the code above, I’m not exactly sure what must come in ??? The initial value is “– Select –“. If the user clicks on the submit button and the dropdown’s value is still “– Select –” then it must display a message.
I am also using EF4. Please can someone advise as to what to do. Code samples would be appreciated.
Thanks.
If your View is strongly typed ie:
Then the ??? in your view code should be a lambda expression referring to the items in your ViewModel. (I assume your ViewModel’s Application object has a property that is going to be assigned a value based on the drop down list?)
I’ve assumed your application object has an AccountType property, For example:
??? should be something like:
The value from the drop down list will populate the AccountType property on your Application model and will be populated with the AccountTypes from your ViewModel.
Hope this helps.
— EDIT —
On your Application model, use the namespace:
Above your AccountTypes property, add
I think this should work.