如何使用mvc 4中数据库中的值添加自动增量下拉列表? [英] How to add an auto incremental dropdown list using the value from database in mvc 4?
本文介绍了如何使用mvc 4中数据库中的值添加自动增量下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过从database中选择数据,将自动增量下拉列表添加到mvc razor视图的创建页面。我可以这样做吗?
解决方案
控制器 - > ActionResult Create()
//从数据库获取国家/地区列表列表
ViewBag.Country = new SelectList( db.Countries.ToList (),ID,CountryName,ID);
现在在设计页面中将viewbag数据作为国家/地区列表
视图 - > Create.cshtml
@ Html.DropDownList(Country,ViewBag.Country as SelectList,
Select Country,new {style =width:200px})
i'm trying to add an auto incremental drop-down list to the create page of mvc razor view by picking data from database.how can i do it?
解决方案
Controllers -> ActionResult Create()
//Get list of Country list from database
ViewBag.Country = new SelectList(db.Countries.ToList(), "ID", "CountryName", "ID");
Now in design page get viewbag data as country list
Views -> Create.cshtml @Html.DropDownList("Country", ViewBag.Country as SelectList, "Select Country", new { style = "width:200px" })
这篇关于如何使用mvc 4中数据库中的值添加自动增量下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文