我是MVC新手并尝试了一些事情,所以如果有人能告诉我哪里出错了,我会很感激。我将它从VB转换为有效,但现在,在C#中它没有。也许我的转换很糟糕,但类似的代码在VB中工作。
我将模型传递给索引(稍后会渲染几个部分,我将构建一个ViewModel来保存每种不同类型的模型)。然后,我尝试将模型传递给Partial,但是Partial无法在foreach循环中访问模型。
下面是代码。任何帮助将不胜感激。
控制器:
public ActionResult HospiceSummary(int? id)
{
IEnumerable<PatAdm> patadms = (from pa in db.PatAdms where pa.AdmNum == id select pa).ToList();
return View(patadms);
}
查看:
@model IEnumerable<PatAdm>
@{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>.... Some other code up here</h2>
<h2>Hospice</h2>
<div class="container-fluid">
@Html.Partial("~/Views/Shared/_PatientAdmissionSearch.cshtml", Model)
</div>
部分:
@model IEnumerable<PatAdm>
<div class="row">
<div class="col-xs-12">
<div class="table-header">
Find a Patient
</div>
</div> @*col-xs-12*@
</div> @*row1*@
<div class="row">
@using (Html.BeginForm("Index", "Admission", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="col-md-4">
<div class="input-append">
<input type="text" class="span2 search-query" id="query" name="query">
<button type="submit" class="btn"><i class="icon-search"></i></button>
</div>
</div>
<table class="table">
<tr>
<th></th>
<th>Patient Name</th>
<th>Acct #</th>
<th>Admit Date</th>
<th>Discharge Date</th>
<th>DOB</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.ActionLink("Select", "HospiceSummary", "Hospice", new {id = item.AdmNum}, new { @class = "label label-info"}) <br />
</td>
<td>@Html.DisplayFor(modelItem => item.Patient.LName), @Html.DisplayFor(modelItem => item.Patient.FName)</td>
<td>@Html.DisplayFor(modelItem => item.AdmNum)</td>
<td>@Html.DisplayFor(modelItem => item.AdmDate)</td>
<td>@Html.DisplayFor(modelItem => item.DischDate)</td>
<td>@Html.DisplayFor(modelItem => item.Patient.DOB)</td>
</tr>
}
</table>
}
</div> @*row2*@
答案 0 :(得分:2)
父视图的模型默认传递给局部视图,因此您不需要显式传递。还要确保父视图上的模型具有值。