如何在 ASP.NET MVC 4 中 return 索引布局的视图和部分视图?
How do I return a View and a Partial View to the Index Layout in ASP.NET MVC 4?
我的家庭控制器和我的 "StatusController" 都创建了 DEBentities 的新实例。家庭控制器 return 是 _db.VMs ViewData.Model 到 _Layout.cshtml 视图,状态控制器 return 是 _db.Jobs ViewDataModel 到 _GetForStatus.cshtml作为局部视图查看。我将 VM ViewData 模型调用到 _Layout 视图,如下所示:
@foreach (var m in ViewData.Model)
{
<li><a href="#">@m.Name</a></li>
}
这工作正常,用数据库中的 VM 名称填充下拉列表。
_GetforStatus。
家庭控制器部分是这样写的:
public ActionResult index()
{
_db = new IntegrationDBEntitires();
ViewData.Model = _db.VMs.ToList();
return View();
}
StatusController是这样写的:
public PartialViewResult _GetforStatus()
{
_db = new OntegrationDBEntities();
ViewData.Model = _db.Jobs.ToList();
return PartialView();
}
_GetforStatus视图是这样写的:
@model IntegrationWeb.Models.Job
<div class="progress progress-striped active">
<div class="progress-bar" style="width: @((Model.IS_Progress / Model.IS_Total)*100)%"></div>
</div>
@Html.Action("_GetforStatus", "StatusController")
在 _Layout 视图中这样调用:\
@Html.Partial("~/Views/_GetforStatus.cshtml")
我在这里遇到错误。 “传入字典的模型项的类型为 'System.Collections.Generic.List' 1[IntegrationWeb.models.VM]',但字典需要模型项类型 'IntegrationWeb.Models.Job'。
将两个不同的 DEBentities 拉入布局视图似乎存在冲突。
我如何在 ASP.NET MVC 4 中 return 索引布局的视图和部分视图?
更新:我只是将 Action 放在家庭控制器中,因为我不明白为什么它找不到我的控制器 "StatusController"。现在我使用 @{Html.RenderAction("_GetforStatus");} "The Model Item passed into the dictionary is type 'System.Collections.Generic.List '1[IntegrationWeb.models.Job]', but this dictionary requires a model item of type 'IntegrationWeb.models.Job'." 得到了一个不同的错误,有人知道这里发生了什么吗?
分部视图从父视图继承模型,除非您专门传递一个 属性,或创建另一个对象传递给它。它们也处理相同的请求。因此,在您的示例中,局部视图的控制器操作甚至没有被执行。您需要做的是使用 RenderAction。
@{ Html.RenderAction("_GetforStatus","ControllerName"); }
这将允许您使用自己的模型和单独的请求执行视图。
而不是
@Html.Partial("~/Views/_GetforStatus.cshtml")
简单做
@Html.Action("_GetForStatus", "MyController")
Html.Partial
实际上并没有触发动作,它只是呈现 cshtml 文件。
我的家庭控制器和我的 "StatusController" 都创建了 DEBentities 的新实例。家庭控制器 return 是 _db.VMs ViewData.Model 到 _Layout.cshtml 视图,状态控制器 return 是 _db.Jobs ViewDataModel 到 _GetForStatus.cshtml作为局部视图查看。我将 VM ViewData 模型调用到 _Layout 视图,如下所示:
@foreach (var m in ViewData.Model)
{
<li><a href="#">@m.Name</a></li>
}
这工作正常,用数据库中的 VM 名称填充下拉列表。 _GetforStatus。 家庭控制器部分是这样写的:
public ActionResult index()
{
_db = new IntegrationDBEntitires();
ViewData.Model = _db.VMs.ToList();
return View();
}
StatusController是这样写的:
public PartialViewResult _GetforStatus()
{
_db = new OntegrationDBEntities();
ViewData.Model = _db.Jobs.ToList();
return PartialView();
}
_GetforStatus视图是这样写的:
@model IntegrationWeb.Models.Job
<div class="progress progress-striped active">
<div class="progress-bar" style="width: @((Model.IS_Progress / Model.IS_Total)*100)%"></div>
</div>
@Html.Action("_GetforStatus", "StatusController")
在 _Layout 视图中这样调用:\
@Html.Partial("~/Views/_GetforStatus.cshtml")
我在这里遇到错误。 “传入字典的模型项的类型为 'System.Collections.Generic.List' 1[IntegrationWeb.models.VM]',但字典需要模型项类型 'IntegrationWeb.Models.Job'。
将两个不同的 DEBentities 拉入布局视图似乎存在冲突。
我如何在 ASP.NET MVC 4 中 return 索引布局的视图和部分视图?
更新:我只是将 Action 放在家庭控制器中,因为我不明白为什么它找不到我的控制器 "StatusController"。现在我使用 @{Html.RenderAction("_GetforStatus");} "The Model Item passed into the dictionary is type 'System.Collections.Generic.List '1[IntegrationWeb.models.Job]', but this dictionary requires a model item of type 'IntegrationWeb.models.Job'." 得到了一个不同的错误,有人知道这里发生了什么吗?
分部视图从父视图继承模型,除非您专门传递一个 属性,或创建另一个对象传递给它。它们也处理相同的请求。因此,在您的示例中,局部视图的控制器操作甚至没有被执行。您需要做的是使用 RenderAction。
@{ Html.RenderAction("_GetforStatus","ControllerName"); }
这将允许您使用自己的模型和单独的请求执行视图。
而不是
@Html.Partial("~/Views/_GetforStatus.cshtml")
简单做
@Html.Action("_GetForStatus", "MyController")
Html.Partial
实际上并没有触发动作,它只是呈现 cshtml 文件。