2012-10-09 103 views
0

在强类型视图中,我可以不呈现强类型部分吗?在强类型视图中显示强类型部分

我收到以下错误,当我尝试:

传递到字典的模型产品 类型的“System.Data.Linq.Table 1[UI.Models.vwProject]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1 UI.Models.ProjectStatus]” 。

我既填充使用ViewData.Model

public ActionResult Index() 
{ 
    ViewData.Model = project.vw_Projects; 
    return View(); 
} 

public ActionResult ProjectStatus() 
{ 
    ViewData.Model = project.ProjectStatus; 
    return View(); 
} 

这里的意见是我的看法:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<UI.Models.vwProject>>" %> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <div> 
    <table> 
    <tr> 
     <th> 
      Project 
     </th> 
     <th> 
      Hours 
     </th> 
    </tr> 
    <tr> 
     <td> 
      <%= Html.Encode(item.ProjectCode) %> 
     </td> 
     <td> 
      <%= Html.Encode(item.ProjectHours) %> 
     </td> 
    </tr> 

<div> 
<% Html.RenderPartial("ProjectStatus"); %> 
</div> 

</asp:Content> 

这里是我的部分:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<UI.Models.ProjectStatus>>" %> 

    <table> 
     <tr> 
      <th> 
       Code 
      </th> 
      <th> 
       Status 
      </th> 
     </tr> 

    <% foreach (var item in Model) { %> 

     <tr> 
      <td> 
       <%= Html.Encode(item.ProjectCode) %> 
      </td> 
      <td> 
       <%= Html.Encode(item.ProjectStatus) %> 
      </td> 
     </tr> 

    <% } %> 

    </table> 

我有点与强烈地显示强类型/动态偏分量相混淆类型或动态视图。有人可以帮我解决这个问题吗?

+0

难道你需要在一个特定对象到'RenderPartial'通过?如果你不知道,它会从调用视图中获取模型。 – TLS

+0

@TLS是的,我确实需要将特定的对象传递给RenderPartial – user793468

回答

1

如果你渲染一个partial,它不会打你控制器动作只是使用partent的视图模型默认渲染视图。

如果你想打电话给你的控制器动作ProjectStatus那么你需要什么是RenderAction方法:

<% Html.RenderAction("ProjectStatus"); %> 

一篇好文章关于When to use RenderAction vs RenderPartial with ASP.NET MVC

+0

何时适合使用动态视图和偏好? – user793468

+1

@ user793468如果没有给定的上下文,很难回答你的问题,也许你应该问一个不同的问题......我个人从来没有使用动态视图只有强类型的......我们使用partials当视图可以呈现与手头的模型并且不需要额外的代码在控制器中运行。 – nemesv

2

当然可以。但是,如果您不将模型传递给RenderPartial,它将使用视图中的模型。 所以,你需要做这样的事情:

Html.RenderPartial("ProjectStatus", new List<ProjectStatus>()); %>