【问题标题】:Using two partial views in the same view在同一个视图中使用两个局部视图
【发布时间】: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 的实例传递给它,但它需要一个 typeof StatisticsModel 的模型。而且您还使用_StatisticsPartialView 作为Statistics.cshtml 中的布局,这也没有意义。
  • 你能显示_StatisticsPartialView.cshtml 的视图吗(你所显示的只是Statistics.cshtml 的视图,我什至不确定这与你的问题有什么相关性)
  • @StephenMuecke,你的观点是正确的,我认为局部视图包含不同的模型,而主视图正在向它发送另一个模型。
  • @StephenMuecke 我现在添加了部分视图。
  • 您需要使用完全限定名称 - new ASD.Models.StatisticsModel() 或在视图顶部添加 @using ASD.Models; 语句

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


【解决方案1】:

您可以通过如下定义其完整路径来获取它:

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

或者交替

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

希望对你有帮助!!

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

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

【讨论】:

  • 我仍然收到相同的错误:附加信息:未找到局部视图“~/Views/Shared/_StatisticsPartialView.cshtml”或没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/Shared/_StatisticsPartialView.cshtml
  • @ErikSellberg,您能否分享您的解决方案资源管理器屏幕截图,指出该视图所在的共享文件夹?
  • 实际上部分视图名称有错字,现在将使用新错误更新顶部的主帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 1970-01-01
  • 2017-09-01
相关资源
最近更新 更多