MVC Dropdownlistfor<>

后端 未结 4 666
轮回少年
轮回少年 2021-02-10 14:41

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:20

    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

提交回复
热议问题