MVC Dropdownlistfor<>

后端 未结 4 772
小鲜肉
小鲜肉 2021-02-10 14:37

I just started a project in MVC. I am new to MVC asp.net. I want to add a dropdown list box like I used to add in asp.net.

code glimpses in asp.net for Dropdownlist box

4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-10 15:08

    If you want to avoid the usage of model, you may simply use a ViewBag. in your action:

    ViewBag.DirectorId = new SelectList(ListOfItemsToAdd, "dataValueField", "dataTextField", movie.DirectorId);
    

    and in the view:

    <%= Html.DropDownList("VariableNameToPost", ViewBag.DirectorId) //for asp.net view engine %> 
    @Html.DropDownList("VariableNameToPost", ViewBag.DirectorId) // for razor view engine
    

    and the action that would accept the variable would then have something like this:

    public ActionResult theAction(string VariableNameToPost){...}
    

    refferences:

    • Select List
    • Drop Down List

提交回复
热议问题