在C#MVC razor中绑定数据库中的dropdownlist [英] bind dropdownlist from database in C# MVC razor
本文介绍了在C#MVC razor中绑定数据库中的dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#MVC剃刀代码中绑定数据库中的dropdownlist?
谢谢。
How to bind dropdownlist from database in C# MVC razor code?
Thank You.
推荐答案
语法:
Syntax:
@Html.DropDownList(<drodownlist name="">, IEnumerable Selectlist, html Attribute) </drodownlist>
Ex: -
Ex:-
@Html.DropDownList("lstGroupName", ViewBag.GroupName as MultiSelectList, new { @class = "groupDropdownlist" })
在这些中我将列表绑定到视图控制器&将它分配给viewbagGroupName。
如果您有任何疑问,请告知我们。
In these I am bind a list into that views controller & assign it to to the viewbag "GroupName".
In these if you have any question, Please let me know.
在视图中
in view
@Html.DropDownList("dropDownName",(SelectList)ViewBag.Category, "--Select category--", new { @class = "ClassName" })
在控制器中
in controller
ViewBag.Category= new SelectList(dbEntities.Categories, "id", "category");// select statement in ef
希望这个帮助。如果有帮助请投票。
问候
hope this help. please vote if helped.
regards
这篇关于在C#MVC razor中绑定数据库中的dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文