Pergunta

I am using the aspx view engine. I made it with SelectList and Html.DropDownList but populating should come from the model, not the controller and that I need to get the value of the selected item, so I need DropDownListFor. I've seen answers but I cannot get from there what kind of parameters were used.

Model:

public List<SelectListItem> food = new List<SelectListItem>();

Controller:

food.Add(new SelectListItem() { Text = "Banana Cake", Value = "BC" });
food.Add(new SelectListItem() { Text = "Apple Waffle", Value = "WF" });
ViewData[" Desserts "] = new SelectList(foodsample.Food);

View:

<%= Html.DropDownListFor(x => x.food, ViewData["Desserts"] as SelectList)%>

No error before and after runtime although the DropDownList displays all values with this text 'System.Web.Mvc.SelectListItem'

Foi útil?

Solução

I think you mean to say ViewData["Desserts"] = food;

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top