我正在努力让一些基本的下拉框正常工作,现在我遇到的唯一问题是 TTTime 的索引视图显示所选值的 Id,而不是该值的字符串“proj_code”。我已经从我的模型中自动生成了控制器和视图,然后对它们进行了编辑以给 proj_code 字段提供一个下拉列表。这是代码的相关部分,请问我是否可以提供其他任何帮助。
下拉列表的 TTTime 控制器代码:
public ActionResult Details(int id = 0)
{
TTTime tttime = db.TTTime.Find(id);
if (tttime == null)
{
return HttpNotFound();
}
var query = from c in db.TTSites select c;
ViewBag.proj_code = new SelectList(query, "ID", "client_id", tttime.proj_code);
return View(tttime);
}
//
// GET: /Time/Create
public ActionResult Create()
{
TTTime tttime = new TTTime();
tttime.CreatedDate = DateTime.Now;
var query = from c in db.TTSites select c;
ViewBag.proj_code = new SelectList(query, "ID", "client_id");
return View(tttime);
}
为 cust_id 创建/编辑/删除视图代码:
<div class="editor-label">
@Html.LabelFor(model => model.proj_code)
</div>
<div class="editor-field">
@Html.DropDownList("proj_code", String.Empty)
@Html.ValidationMessageFor(model => model.proj_code)
</div>
仅给出 id 值但应显示 proj_code 字符串的索引视图代码:
@model IEnumerable<TimeTracker.Models.TTTime>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.CreatedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.employee)
</th>
</th>
<th>
@Html.DisplayNameFor(model => model.hrs_calc)
</th>
<th>
@Html.DisplayNameFor(model => model.cust_id)
</th>
<th>
@Html.DisplayNameFor(model => model.description)
</th>
<th>
@Html.DisplayNameFor(model => model.proj_code)
</th>
<th>
@Html.DisplayNameFor(model => model.type)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.CreatedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.employee)
</td>
<td>
@Html.DisplayFor(modelItem => item.hrs_calc)
</td>
<td>
@Html.DisplayFor(modelItem => item.cust_id)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
<td>
@Html.DisplayFor(modelItem => item.proj_code)
</td>
<td>
@Html.DisplayFor(modelItem => item.type)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}