在同一视图中使用两个部分视图

时间:2015-11-11 07:05:44

标签: c# html asp.net-mvc razor

更新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);
 }

我对此很新,所以任何帮助都会受到赞赏。 :)

1 个答案:

答案 0 :(得分:3)

你可以通过定义如下的完整路径来获得它:

@Html.Partial("~/Views/Shared/_StatisticsPartialView.cshtml")

或者替代

@{ Html.RenderPartial("~/Views/Shared/_StatisticsPartialView.cshtml"); }

希望这会有所帮助!!

修改:根据新的更改,您必须将正确的模型传递给当前获取的LogModelVM,因此请在下面使用:

@{Html.RenderPartial("_StatisticsPartialView", new ASD.Models.StatisticsModel());}