C# 使用数据库中的值填充下拉列表 - MVC4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17948371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill Dropdownlist with values from database - MVC4
提问by user2232273
i have a table in my database which i select all rows to fill it in my dropdownlist in my view.
我的数据库中有一个表,我选择所有行以将其填充到我视图中的下拉列表中。
i can′t understand how can i fill the values in there.
我无法理解如何填写那里的值。
someone can give me a hand?
有人可以帮我一把吗?
My code:
我的代码:
Model:
模型:
public class MyList
{
public int id { get; set; }
public string name{ get; set; }
}
public class Empresas
{
public static IEnumerable<MyList> Getmyinformation()
{
var list = new List<MyList>();
string connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var con = new SqlConnection(connection))
{
con.Open();
using (var command = new SqlCommand("SELECT * FROM mytable", con))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string Name= reader[1] as string;
list.Add(new MyList() { name= Name});
}
}
con.Close();
}
return list;
}
}
public class DefaultConnection : DbContext
{
public DbSet<MyList> lat { get; set; }
}
Controller:
控制器:
private DefaultConnection db = new DefaultConnection();
public ActionResult Add()
{
return View(db.lat.ToList());
}
View:
看法:
@Html.DropDownListFor("-- Select --", new SelectList(""))
<=== ???? i dont know
@Html.DropDownListFor("-- Select --", new SelectList(""))
<===????我不知道
回答by ataravati
Create a ViewModel like this:
像这样创建一个 ViewModel:
public class ListViewModel
{
public MyList MyList { get; set; }
public int SelectedId { get; set; }
}
Then, change your Action to this:
然后,将您的操作更改为:
public ActionResult Add()
{
var viewModel = new ListViewModel { MyList = db.lat.ToList() };
return View(viewModel);
}
And, then, this is what you will have in your View:
然后,这就是您将在您的视图中看到的内容:
@model MyApp.ViewModels.ListViewModel
@Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.MyList as IEnumerable, "Id", "Name"))
回答by Jeet Bhatt
Try this,
尝试这个,
View :-
看法 :-
@Html.DropDownListFor(m => m.CustomerId, Model.customerNameList, "--Select--")
Controller:-
控制器:-
public ActionResult CustomerInfo()
{
var List = GetCustomerName();
ViewBag.CustomerNameID = new SelectList(List, "CustomerId", "customerName");
ViewBag.RegisterItems = GetAllRegisterData();
return View();
}
public List<CustomerModel> GetCustomerName()
{
// Customer DropDown
using (dataDataContext _context = new dataDataContext())
{
return (from c in _context.Customers
select new CustomerModel
{
CustomerId = c.CID,
customerName = c.CustomerName
}).ToList<CustomerModel>();
}
}
Model:
模型:
public class CustomerModel
{
public int CustomerId { get; set; }
[StringLength(9), Required, DisplayName("Social security number")]
[RegularExpression(@"\d{3}-\d\d-\d{4}", ErrorMessage = "Invalid social security number")]
public string customerName { get; set; }
public List<MyListItems> customerNameList { get; set; }
}
回答by Elvin Mammadov
In my practice I create dropdownlist as follow:
在我的实践中,我创建下拉列表如下:
first I create view model
首先我创建视图模型
public class MyObj
{
public int id { get; set; }
public string name{ get; set; }
}
// viewmodel
public class MyviewModel
{
public IQuerable<MyObj> MyObjs{get;set;}
public Other Other{get;set;}
}
then I pass this model from controller to view
然后我将这个模型从控制器传递给视图
private DefaultConnection db = new DefaultConnection();
public ActionResult Index()
{
var drop = new MyviewModel
{
MyObjs = db.MyObjs,// selecting table...
Other = new Other
}
return View(drop);
}
in Controller
在控制器中
@Html.DropDownListFor(model => model.Other.MyObjId, new SelectList(Model.MyObjs , "id", "name","--select--"))
回答by Jeyhun Rahimov
Simply in controller type:
只需在控制器类型中:
ViewBag.CategoryList = new SelectList(db.Categories.ToList(), "Id", "Name");
And in view write:
并在视图中写:
@Html.DropDownListFor(model => model.CategoryId, ViewBag.CategoryList as IEnumerable<SelectListItem>, new { @class = "anyclass" })