문제

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'

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top