Html.DropDownList Selected Value Not Working (Using Constructor with IEnumerable)

后端 未结 8 2225
盖世英雄少女心
盖世英雄少女心 2021-02-18 19:02

I have an issue where the selected value is not working for the Html.DropDownList helper method. See below:

This is My Controller:

public ActionResult Ed         


        
8条回答
  •  天命终不由人
    2021-02-18 19:21

    I tried lot of stuff but eventually got it working like this

    In controller

    int valittu_tila = 1;
    var tapahtumantilat = new List();
    
    tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa kirjataan", Value = "0" });
    tapahtumantilat.Add(new SelectListItem { Text = "Odottaa jonossa", Value = "1"});
    tapahtumantilat.Add(new SelectListItem { Text = "Tapahtumaa käsitellään", Value = "2"});
    
    ViewBag.tilalista = new SelectList(tapahtumantilat, "Value", "Text", valittu_tila);
    

    And then in view I just put (I have the onchange event and class for dropdown too here but you can remove those if you want).

    @Html.DropDownList("tuki_tila", ViewBag.tilalista as SelectList, "-- valitse tila --", new { @onchange = "executeticketsearch();", @class = "haku_dropdowns" })
    

    (Sorry about finnish in the text values and variable names, but those shouldn't matter :D)

提交回复
热议问题