How to get data to Dropdownlist from database in html view

前端 未结 2 1001
滥情空心
滥情空心 2021-01-29 15:14

I am creating an web page in which have a Dropdownlist. I have to retrieve data for the drop_down_list from the database. Is there any way to get data from the data

2条回答
  •  既然无缘
    2021-01-29 15:21

    It is just a simple two step process:

    Step1 :Action method code

    public ActionResult Index()  
    {
      ViewBag.users = db.users.ToList();
    }
    

    Step2: cshtml code

    @Html.DropDownListFor(model => model.someId, new SelectList(ViewBag.users, "userId", "userName"), "Select users")
    

    Note: with this, you can bind n number of data from the database to dropdownlist

    Hope it was useful

    Thanks

    Karthik

提交回复
热议问题