[英]How to bind dropdown list in ASP.NET MVC from SQL Server database?
I don't know how to make view from this code to show data in dropdown list.我不知道如何从此代码查看以在下拉列表中显示数据。 Please do tell me what changes I can make to solve my problem.请告诉我我可以做出哪些改变来解决我的问题。 I have tried some view but this causes errors.我尝试了一些视图,但这会导致错误。
Model:模型:
public class Cities
{
public int cityid { get; set; }
public string Description { get; set; }
}
Controller:控制器:
public ActionResult Dropdownlist()
{
List<Citites> cityname= new List<Citites>();
string constring = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
SqlCommand cmd = new SqlCommand("SELECT Cityid, Description FROM Cities", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Citites ci = new Citites();
ci.cityid = Convert.ToInt32(rdr["Cityid"]);
ci.Description = rdr[1].ToString();
cityname.Add(ci);
}
return View(cityname);
}
View:看法:
@model IEnumerable< RegisterForm_crud_mvc_.Models.Citites>
@{
ViewBag.Title = "Dropdownlist";
}
<h2>Dropdownlist</h2>
@Html.DropDownListFor(model => model.cityid, Model.citynanme)
You have a bunch of issues in your code.您的代码中有很多问题。 Your view is strongly typed to a list of City objects and you are trying to use Model.citynanme
.您的视图被强类型Model.citynanme
City 对象列表,并且您正在尝试使用Model.citynanme
。 Your model is the list of city object and the list does not have a cityname property ( but each item in the list has)!您的模型是 city 对象列表,该列表没有 cityname 属性(但列表中的每个项目都有)!
What you should ideally do is , Create a view model to represent the data needede in your view.理想情况下,您应该做的是,创建一个视图模型来表示视图中所需的数据。 In your case, you need a property to store the selected option value and another for data needed to build the options for the SELECT element.在您的情况下,您需要一个属性来存储选定的选项值,另一个用于为 SELECT 元素构建选项所需的数据。 In your case it is the list of cities.在您的情况下,它是城市列表。
public class MyViewModel
{
public List<SelectListItem> Cities { set;get;}
public int SelectedCityId { set;get;}
}
Now in your GET action, you create an object of this, populate the Cities property by reading your table and send the view model object to the view.现在在您的 GET 操作中,您创建一个对象,通过读取您的表来填充 Cities 属性并将视图模型对象发送到视图。
public ActionResult Create()
{
var vm = new MyViewModel();
vm.Cities = GetCities();
return View(vm);
}
private List<SelectListItem> GetCities()
{
var options = new List<SelectListItem>();
var constring = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;
using (var c = new SqlConnection(constring))
{
using (var cmd = new SqlCommand("select Cityid, Description FROM Cities", c))
{
c.Open();
var rdr = cmd.ExecuteReader();
options = new List<SelectListItem>();
while (rdr.Read())
{
var o = new SelectListItem
{
Value = rdr.GetInt32(rdr.GetOrdinal("Cityid")).ToString(),
Text = rdr.GetString(rdr.GetOrdinal("Description"))
};
options.Add(o);
}
}
}
return options;
}
Now in your view, which is strongly typed to MyViewModel, you will use Html.DropDownListfor
helper method where you will use the Cities
property as the selectList parameter.现在,在 MyViewModel 强类型化的视图中,您将使用Html.DropDownListfor
helper 方法,在该方法中您将使用Cities
属性作为 selectList 参数。
@model MyViewModel
@Html.DropDownListFor(model => model.SelectedCityId, Model.Cities)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.