C# MVC@Html.Partial-两种型号
我试图在视图中使用局部视图。第一个视图需要一个模型,而局部视图需要相同模型的IEnumerable。我得到以下错误:C# MVC@Html.Partial-两种型号,c#,asp.net-mvc-3,razor,telerik-mvc,C#,Asp.net Mvc 3,Razor,Telerik Mvc,我试图在视图中使用局部视图。第一个视图需要一个模型,而局部视图需要相同模型的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]'
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 MyVDC.Models.LogBook
@{
}
@使用(Html.BeginForm())
{
@Html.ValidationSummary(true)
航海日志
@(Html.Telerik().DatePicker())
.姓名(“日期”)
)
}
@Html.Partial(“网格”)
第二种观点:
@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()
)
@model IEnumerable
@(Html.Telerik().Grid(模型)
.名称(“网格”)
.列(列=>
{
columns.Bound(o=>o.Day).Format(“{0:MM/dd/yyyy}”).Width(120);
})
.DataBinding(DataBinding=>DataBinding.Ajax().Select(“网格”、“日志”))
.Pageable()
.Sortable()
.可过滤()
)
谢谢你的建议。解决方法,请参见下文。我使用ViewData解决了这个问题,如下所示: 在控制器中:
ViewData["Log"] = db.LogBooks.ToList().Where(c => c.PHN == phn);
他认为:
@(Html.Telerik().Grid((IEnumerable<MyVDC.Models.LogBook>)ViewData["Log"])
@(Html.Telerik().Grid((IEnumerable)ViewData[“Log”])
希望这对其他人有所帮助。使用
Html.Render
的问题是与两个视图关联的类型需要相同。您可以使用Html.RenderAction
而不是Html.Render
。使用此方法时,与视图关联的类型无关紧要。您将需要类似的内容:
@{ Html.RenderAction("Grid"); }