[英]Fill Dropdownlist with values from database - MVC4
我的數據庫中有一個表,我選擇了所有行以將其填充到視圖中的下拉列表中。
我不明白如何在其中填寫值。
有人可以幫我嗎?
我的代碼:
模型:
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; }
}
控制器:
private DefaultConnection db = new DefaultConnection();
public ActionResult Add()
{
return View(db.lat.ToList());
}
視圖:
@Html.DropDownListFor("-- Select --", new SelectList(""))
<=== ???? 我不知道
只需在控制器類型中:
ViewBag.CategoryList = new SelectList(db.Categories.ToList(), "Id", "Name");
並在視圖中寫道:
@Html.DropDownListFor(model => model.CategoryId, ViewBag.CategoryList as IEnumerable<SelectListItem>, new { @class = "anyclass" })
在我的實踐中,我創建下拉列表,如下所示:
首先,我創建視圖模型
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;}
}
然后我將這個模型從控制器傳遞給視圖
private DefaultConnection db = new DefaultConnection();
public ActionResult Index()
{
var drop = new MyviewModel
{
MyObjs = db.MyObjs,// selecting table...
Other = new Other
}
return View(drop);
}
在控制器中
@Html.DropDownListFor(model => model.Other.MyObjId, new SelectList(Model.MyObjs , "id", "name","--select--"))
創建一個這樣的ViewModel:
public class ListViewModel
{
public MyList MyList { get; set; }
public int SelectedId { get; set; }
}
然后,將您的操作更改為:
public ActionResult Add()
{
var viewModel = new ListViewModel { MyList = db.lat.ToList() };
return View(viewModel);
}
然后,這就是視圖中的內容:
@model MyApp.ViewModels.ListViewModel
@Html.DropDownListFor(model => model.SelectedId, new SelectList(Model.MyList as IEnumerable, "Id", "Name"))
嘗試這個,
查看:-
@Html.DropDownListFor(m => m.CustomerId, Model.customerNameList, "--Select--")
控制器:
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>();
}
}
模型:
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; }
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.