Reputation: 5377
I'm trying to create a drop down list in an ASP.NET MVC3 view based on a list of allowed values linked to the model.
So far, in my model, I've got:
namespace NS
{
public class Model
{
public Model()
{
Status = new List<SelectListItem>();
Status.Add(new SelectListItem { Text = "New", Value = "New" });
Status.Add(new SelectListItem { Text = "PaymentPending", Value = "PaymentPending" });
Status.Add(new SelectListItem { Text = "PaymentProcessed", Value = "PaymentProcessed" });
Status.Add(new SelectListItem { Text = "Dispatched", Value = "Dispatched" });
Status.Add(new SelectListItem { Text = "Complete", Value = "Complete" });
Status.Add(new SelectListItem { Text = "Cancelled", Value = "Cancelled" });
}
public List<SelectListItem> Status { get; set; }
} // class Model
} // NS
(obviously trimming unneccessary stuff out)
Then in my view I've got:
@model NS.Model
@Html.DropDownListFor(Model.Status)
As looking at answers on SO seems to suggest. But I get an error:
Compiler Error Message: CS1501: No overload for method 'DropDownListFor' takes 1 arguments
Any hints much appreciated.
Upvotes: 3
Views: 1217
Reputation: 31033
the error message is pretty self-explanatory, the DropDownListFor helper takes two arguments.
modify your model to have a property to contain the selected value
public class Model
{
public Model() {
Status = new List<SelectListItem>();
Status.Add(new SelectListItem { Text = "New", Value = "New" });
Status.Add(new SelectListItem { Text = "PaymentPending", Value = "PaymentPending" });
Status.Add(new SelectListItem { Text = "PaymentProcessed", Value = "PaymentProcessed" });
Status.Add(new SelectListItem { Text = "Dispatched", Value = "Dispatched" });
Status.Add(new SelectListItem { Text = "Complete", Value = "Complete" });
Status.Add(new SelectListItem { Text = "Cancelled", Value = "Cancelled" });
}
public List<SelectListItem> Status { get; set; }
public string SelectedVal{get;set;}
}
then in the view
@NS.Model
@Html.DropDownListFor(x=> x.SelectedVal, x.Status)
Upvotes: 5
Reputation: 4475
The first parameter is the selected value of the dropdown list from the model. The second value is the list of statuses.
@Html.DropDownListFor(x=> x.SelectedValue, Model.Status)
Upvotes: 0