MVC DropDownList SelectedValue not displaying correctly

后端 未结 7 954
甜味超标
甜味超标 2020-12-01 12:40

I tried searching and didn\'t find anything that fixed my problem. I have a DropDownList on a Razor view that will not show the the item that I have marked as Selected in th

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 12:46

    Please find sample code below.

    public class Temp
        {
            public int id { get; set; }
            public string valueString { get; set; }
        }
    

    Controller

    public ActionResult Index()
            {
                // Assuming here that you have written a method which will return the list of Temp objects.
                List temps = GetList();
    
                var tempData = new SelectList(temps, "id", "valueString",3);
                ViewBag.Statuses = tempData;
    
                return View();
            }
    

    View

     @Html.DropDownListFor(model => model.id, (SelectList)ViewBag.Statuses)
        @Html.ValidationMessageFor(model => model.id)
    

提交回复
热议问题