【发布时间】:2016-02-12 06:11:18
【问题描述】:
更新 2:局部视图
@model ASD.Models.StatisticsModel
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
{
<table id="statisticstable">
<thead>
<tr>
<th>Hour</th>
<th>User</th>
<th>Customer</th>
<th>Order</th>
<th>Rows</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
</table>
}
更新 1
partialView 名称有错字。修复它并出现此错误:
附加信息:传入字典的模型项的类型为“ASDWebPortalMVC.Models.LogModelVM”,但此字典需要“ASD.Models.StatisticsModel”类型的模型项。
我正在使用@html.RenderPartial 来渲染局部视图。
LogModelsController.cs -(连接到 LogModel)
[HttpPost]
public PartialViewResult LogPartialView()
{
// Some other stuff
LogModelVM LMVM = new LogModelVM();
return PartialView("_LogPartialLayout", LMVM);
}
现在我想添加另一个局部视图,使用不同的模型 (StatisticsModel)
LogLayout.cshtml
@model ASDMVC.Models.LogModelVM
@* This is the working PartialView *@
<div id="log" class="tab">
<h1>Log</h1>
@using (Ajax.BeginForm("LogPartialView", "LogModelsController",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divLogs"
}, new
{
id = "NewTableId"
}))
{
<p>@Html.TextBox("SearchString",null, new { @placeholder = "Message" })</p>
if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
{
<p>
@Html.DropDownListFor(m => m.SelectedCustomer, Model.CustomerList, new { @id = "logdropdownlabel" })
</p>
}
<p><input type="submit" class="standardbutton logsearch" name="submit" value="Search" /></p>
}
<div id="divLogs">
@RenderBody()
@Html.Raw(ViewBag.Data)
@{Html.RenderPartial("_LogPartialLayout");}
</div>
</div>
@* This is the non-working PartialView. *@
<div id="statistics" class="tab">
<h1>Statistics</h1>
<div id="statistics">
@{Html.RenderPartial("_StatisticsPartialView");}
</div>
</div>
StatisticsController.cs(连接到 StatisticsModel)
[HttpPost]
public PartialViewResult Statistics(string conn)
{
StatisticsModel STM = new StatisticsModel();
StatisticsDbContext DbContext = new StatisticsDbContext(conn);
return PartialView("_StatisticsPartialView", STM);
}
我对此很陌生,因此我们将不胜感激。 :)
【问题讨论】:
-
您显示的代码与错误无关。
@{ Html.RenderPartial("_StatisticsPartialView"); }会抛出错误,因为您将LogModelVM的实例传递给它,但它需要一个 typeofStatisticsModel的模型。而且您还使用_StatisticsPartialView作为Statistics.cshtml中的布局,这也没有意义。 -
你能显示
_StatisticsPartialView.cshtml的视图吗(你所显示的只是Statistics.cshtml的视图,我什至不确定这与你的问题有什么相关性) -
@StephenMuecke,你的观点是正确的,我认为局部视图包含不同的模型,而主视图正在向它发送另一个模型。
-
@StephenMuecke 我现在添加了部分视图。
-
您需要使用完全限定名称 -
new ASD.Models.StatisticsModel()或在视图顶部添加@using ASD.Models;语句
标签: c# html asp.net-mvc razor