我正在尝试在视图中使用局部视图。第一个视图需要一个模型,而部分视图需要相同模型的 IEnumerable。我收到以下错误:
The model item passed into the dictionary is of type 'MyVDC.Models.LogBook', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MyVDC.Models.LogBook]'.
在控制器中:
public ActionResult Create(string phn)
{
phn = MySession.Current.PHN;
ViewBag.PHN = MySession.Current.PHN;
LogBook logBook = new LogBook();
try
{
logBook = db.LogBooks.Where(c => c.PHN == phn).OrderByDescending(x => x.Day).First();
}
catch
{
logBook.Day = DateTime.Now.Date;
logBook.PHN = phn;
}
return View(logBook);
}
局部视图的第二个动作:
public ActionResult Grid()
{
string phn = MySession.Current.PHN;
return View(db.LogBooks.ToList().Where(c => c.PHN == phn));
}
第一种观点:
@model MyVDC.Models.LogBook
@{
}
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<input type="hidden" name="PHN" id="PHN" value="@ViewBag.PHN" />
<fieldset>
<legend>LogBook</legend>
@(Html.Telerik().DatePicker()
.Name("Day")
)
<p>
<input type="submit" value="Start New Logbook" class="t-button" />
</p>
</fieldset>
}
<div>
</div>
@Html.Partial("Grid")
第二种观点:
@model IEnumerable<MyVDC.Models.LogBook>
@(Html.Telerik().Grid(Model)
.Name("Grid")
.Columns(columns =>
{
columns.Bound(o => o.Day).Format("{0:MM/dd/yyyy}").Width(120);
})
.DataBinding(dataBinding => dataBinding.Ajax().Select("Grid", "LogBook"))
.Pageable()
.Sortable()
.Filterable()
)
我会很感激你的建议。解决,见下文。