我意识到以前已经提出过这种类型的问题,但是,作为一个新手,我真的很难掌握这个问题。任何帮助表示赞赏。
我有两张表——一张职员表和一张称呼表——我有单独的模型和视图。视图的记录放置在表单中供用户编辑,并通过 LINQ 查询填充。每个员工记录都包含一个称呼 ID 值。我想要一个视图,它会列出所有员工记录,并附上适当的称呼。
我相信我需要的是一个 ViewModel,我也相信它与我用来创建实际数据库表的模型不同。然而,我如何使用这样的视图模型来提取我需要的数据,这让我无法理解。我目前的代码如下:
语境:
public class LibraryContext : DbContext
{
public DbSet<PrmTbl_Staff> PrmTbl_Staffs { get; set; }
public DbSet<PrmTbl_Salutation> PrmTbl_Salutations { get; set; }
}
楷模
public class PrmTbl_Staff
{
public int ID { get; set; }
public int SalutationID { get; set; }
public string Name { get; set; }
public bool Active { get; set; }
public PrmTbl_Staff(int id, int salID, string name, Boolean active)
{
ID = id;
SalutationID = salID;
Name = name;
Active = active;
}
}
public class PrmTbl_Salutation
{
public int ID { get; set; }
public string Desc { get; set; }
public bool Active { get; set; }
public PrmTbl_Salutation(int id, string desc, Boolean active)
{
ID = id;
Desc = desc;
Active = active;
}
}
控制器
private LibraryContext staffDB = new LibraryContext();
public ActionResult Staff()
{
ViewBag.Title = "Parameters - Staff";
var StaffQuery = from st in staffDB.PrmTbl_Staffs
join sal in staffDB.PrmTbl_Salutations on st.SalutationID equals sal.ID
where st.Active == true
orderby st.ID descending
select st;
var count = StaffQuery.Count();
if (count == 0)
{
ViewBag.NullRes = "There are no Results to View!";
}
return View(StaffQuery.ToList());
}
private LibraryContext saltDB = new LibraryContext();
public ActionResult Salutation()
{
ViewBag.Title = "Parameters - Salutations";
var SaltQuery =
(from a in saltDB.PrmTbl_Salutations
where a.Active == true
orderby a.ID descending
select a);
var count = SaltQuery.Count();
if (count == 0)
{
ViewBag.NullRes = "There are no Results to View!";
}
return View(SaltQuery.ToList());
}
看法:
if (ViewBag.NullRes == null) {
using (Html.BeginForm("UpdateStaff", "Parameter", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Update Form</legend>
<table>
<thead>
<tr>
<th>@Html.DisplayNameFor(model => model.ID)</th>
<th>@Html.DisplayNameFor(model => model.SalutationID)</th>
<th>@Html.DisplayNameFor(model => model.Name)</th>
<th>@Html.DisplayNameFor(model => model.Active)</th>
</tr>
</thead>
@foreach (var item in Model)
{
var thisID = item.ID;
var thisSalID = item.SalutationID;
var thisName = item.Name;
var thisActive = item.Active;
// note that intellisense does not pick up any values
// from the Salutations table, so I assume my
// LINQ query is incomplete. For now, I'll just echo the ID
<tr>
<td class="tdsm centered fade"> @thisID </td>
<td> <input type="text" name="@(thisID + ".salID")" value="@thisSalID" /> </td>
<td> <input type="text" name="@(thisID + ".name")" value="@thisName" /> </td>
<td class="tdsm centered">
@{
if (@thisActive)
{
<text><input type="checkbox" name="@(thisID + ".active")" value="true" checked="checked" /></text>
}
else
{
<text><input type="checkbox" name="@(thisID + ".active")" value="true" /></text>
}
}
</td>
</tr>
}
</table>
<p> <input type="submit" value="Update" /> </p>
</fieldset>
}
}
else {
<p>@ViewBag.NullRes</p>
}
因此,总而言之,我希望 Salutations 表中的 Desc 值在视图中显示在员工姓名旁边。理想情况下,我希望所有称呼都显示为下拉菜单,并显示适当的称呼,但如果我能打破最初的问题,我会同样高兴。
请注意,我确实尝试过 ViewModel(就像其他模型一样创建,视图会选择它,但我不知道如何从中提取任何值,或者它们如何相互配对 Salt = > 名称方面...)我还是把它包括在这里。
public class StaffSalutation
{
public List<PrmTbl_Salutation> Salutations { get; set; }
public List<PrmTbl_Staff> Staffs { get; set; }
}
正如我所说,我非常感谢任何帮助 - 解释越多越好!我担心 MVC 的一些概念被我忽略了。
编辑 - 当前控制器
private LibraryContext staffDB = new LibraryContext();
public ActionResult Staff()
{
ViewBag.Title = "Parameters - Staff";
IEnumerable<PrmTbl_Staff> StaffQuery;
StaffQuery = from st in staffDB.PrmTbl_Staffs
join sal in staffDB.PrmTbl_Salutations on st.SalutationID equals sal.ID
where st.Active == true
orderby st.ID descending
select st;
if (StaffQuery.Count() == 0)
{
ViewBag.NullRes = "There are no Results to View!";
}
var viewModel = StaffQuery.Select(staff =>
new StaffSalutation
{
StaffID = staff.ID,
Name = staff.Name,
Salutation = staff.Salutation.Desc,
Active = staff.Active
}
);
return View(viewModel);
}