3

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.

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.