2

I am implementing a backoffice in mvc3 c# and i would like to have a field that is a drop down list with 3 fields "ComingSoon,Out,Showing". These fields are not a part of any class. Do i have to create a helper class ? i have tried the following

<% List<string> foo = new List<string>();
   foo.Add("Showing");
   foo.Add("ComingSoon);"
   foo.Add("Out");

   Html.DropDownList(foo, Model.Status); %>

Status is the field in the db that need to be updated.

3 Answers 3

4
Html.DropDownList("Status", 
    new SelectListItem[]{ new SelectListItem{ Text= "Showing", Value="Showing"},
    //same for others
}); 
Sign up to request clarification or add additional context in comments.

Comments

1

Since you're using model binding, I'd suggest:

<%: Html.DropDownListFor(model=> model.Status, 
                         new List<SelectListItem>() { 
                                new SelectListItem{ Text= "Showing", Value="Showing"},
                                new SelectListItem{ Text= "ComingSoon", Value="ComingSoon"},
                                new SelectListItem{ Text= "Out", Value="Out"}
                         }); %>

For modelbinding, always use the ones with 'For' at the end, it makes the Post-method easier + you get feedback if the name does not exist.

Comments

0

You can create dropdownlist this way.

@{          
    List<KeyValuePair<int, string>> dropdownList =
                                                        new List<KeyValuePair<int, string>>();
                dropdownList.Add(new KeyValuePair<int, string>(0,"Showing"));
                dropdownList.Add(new KeyValuePair<int, string>(1,"ComingSoon"));
                dropdownList.Add(new KeyValuePair<int, string>(2,"Out"));
                SelectList selectList = new SelectList(dropdownList, "key", "value", 0);

}

@Html.DropDownList("foo", selectList)

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.