MVC3 DropDownListFor没有检索项目的命名(ViewBag用于检索) [英] MVC3 DropDownListFor is not retrieving naming of the items (ViewBag Is used for retrieval)
问题描述
亲爱的编程社区,
尝试从数据库/模型中检索称呼列表并将它们放入我的DropDownListFor控件时,我遇到以下问题。 br />
我目前正在使用一个视图包来获取项目列表,但是没有具有正确名称的项目,我得到没有值的项目。由viewbag从数据库中检索的每个项目在我的下拉列表中简单地表示为System.Web.Mvc.SelectListItem。请看下面的代码:
控制器:
//
// 获取:/患者/创建
[授权]
public ActionResult Create()
{
ViewBag.Patient_ward_id = new SelectList(db.Wards, Ward_id, Ward_name< /跨度>);
ViewBag.Patient_salutation_id = new SelectList(db.Salutations, Salutation_id跨度>);
return 查看();
}
//
// POST:/ Patient / Create
[HttpPost]
[授权]
public ActionResult Create(PatientModel patientmodel)
{
if (ModelState.IsValid)
{
db.Patients.Add(patientmodel);
db.SaveChanges();
return RedirectToAction( Index跨度>);
}
return 查看(patientmodel);
}
查看:
< div class = editor-label >
@ Html.LabelFor(model = < span class =code-keyword>> model.Salutation_id, Salutation )
< / div >
< div class = editor-field >
@ * @ Html.DropDownList( Patient_salutation_id)* @
@ * @ Html.DropDownListFor(model = > 型号.Salutat ion_id, new SelectList(Model.Salutation, Salutation_id , Salutation_name))* @
@ Html.DropDownListFor(model = > model.Salutation_id, new SelectList(ViewBag.Patient_salutation_id, Salutation_id))
@ Html.ValidationMessageFor(model = > model.Salutation_id)
< / div >
< div class = editor-label >
@ Html.LabelFor(model = > model.PatientDateOfBirth)
< / div >
< div class = editor-field >
@ Html.EditorFor(model = > model.PatientDateOfBirth)
@ Html.ValidationMessageFor(model = > model.PatientDateOfBirth )
< / div >
欢迎任何意见或建议。
试试这样
为DropDownList添加一个公共类,如下所示
public static class DropDownList< t>
{
public static SelectList LoadItems(IList< t> collection, string value , string text)
{
return new SelectList(collection, value ,text);
}
}
从控制器调用方法,如下所示。
ViewData [ 管理员] =
DropDownList< executive> .LoadItems(
objExecutivesDbContext.Executives.ToList(), ExecutiveId, ExecutiveName);
从视图中调用viewData
< div class = < span class =code-string> editor-field >
@ Html.DropDownListFor(model = > model.ExecutiveId,(IEnumerable< selectlistitem>)ViewData [ 执行], - 选择 - 跨度>)
Dear programming community,
I have the following problem when trying to retrieve a list of salutations from the database/model and putting them in my DropDownListFor control.
I am currently using a viewbag to get the list of items but instead of items with proper names i get items with no values. Each item retrieved from the database by a viewbag is simply represented as "System.Web.Mvc.SelectListItem" in my dropdown list. Please see the code below:
Controller:
//
// GET: /Patient/Create
[Authorize]
public ActionResult Create()
{
ViewBag.Patient_ward_id = new SelectList(db.Wards, "Ward_id", "Ward_name");
ViewBag.Patient_salutation_id = new SelectList(db.Salutations, "Salutation_id");
return View();
}
//
// POST: /Patient/Create
[HttpPost]
[Authorize]
public ActionResult Create(PatientModel patientmodel)
{
if (ModelState.IsValid)
{
db.Patients.Add(patientmodel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(patientmodel);
}
View:
<div class="editor-label">
@Html.LabelFor(model => model.Salutation_id, "Salutation")
</div>
<div class="editor-field">
@* @Html.DropDownList("Patient_salutation_id")*@
@* @Html.DropDownListFor(model => model.Salutation_id, new SelectList(Model.Salutation, "Salutation_id", "Salutation_name"))*@
@Html.DropDownListFor(model => model.Salutation_id, new SelectList(ViewBag.Patient_salutation_id, "Salutation_id"))
@Html.ValidationMessageFor(model => model.Salutation_id)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PatientDateOfBirth)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PatientDateOfBirth)
@Html.ValidationMessageFor(model => model.PatientDateOfBirth)
</div>
Any comments or suggestions are welcome.
Try like this
Add a common class for DropDownList like below
public static class DropDownList<t> { public static SelectList LoadItems(IList<t> collection, string value, string text) { return new SelectList(collection, value, text); } } Call the method from the controller like Below. ViewData["Executives"] = DropDownList<executives>.LoadItems( objExecutivesDbContext.Executives.ToList(), "ExecutiveId", "ExecutiveName "); Call the viewData from the View like below <div class="editor-field"> @Html.DropDownListFor(model => model.ExecutiveId, (IEnumerable<selectlistitem>) ViewData["Executives"], "--Select--")
这篇关于MVC3 DropDownListFor没有检索项目的命名(ViewBag用于检索)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!