C# 在一个视图中呈现多个强类型局部视图?

C# 在一个视图中呈现多个强类型局部视图?,c#,asp.net-mvc,asp.net-mvc-4,razor,visual-studio-2012,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,Visual Studio 2012,我有一个Index.cshtml类: @model ProOptInteractive.ViewModels.InvoicePageViewModel @{ ViewBag.Title = "Index"; } <div>@Html.Partial("ListServices", Model.Services)</div> <div>@Html.Partial("ListProducts", Model.Products)</div>

我有一个Index.cshtml类:

@model ProOptInteractive.ViewModels.InvoicePageViewModel

@{
    ViewBag.Title = "Index";
}

<div>@Html.Partial("ListServices", Model.Services)</div>
<div>@Html.Partial("ListProducts", Model.Products)</div>
<div>@Html.Partial("Invoice", Model.InvoiceViewModel)</div>
@model ProOptInteractive.ViewModels.InvoicePageViewModel
@{
ViewBag.Title=“Index”;
}
@Html.Partial(“ListServices”,Model.Services)
@Html.Partial(“ListProducts”,Model.Products)
@Html.Partial(“发票”,Model.InvoiceViewModel)
这是我的InvoicePageViewModel:

namespace ProOptInteractive.ViewModels
{
    public class InvoicePageViewModel
    {
        public InvoiceViewModel InvoiceViewModel { get; set; }
        public IEnumerable<Service> Services { get; set; }
        public IEnumerable<Product> Products { get; set; }
    }
}
namespace ProOptInteractive.ViewModels
{
公共类InvoicePageViewModel
{
公共InvoiceViewModel InvoiceViewModel{get;set;}
公共IEnumerable服务{get;set;}
公共IEnumerable乘积{get;set;}
}
}
问题是,每当我加载这个视图时,我都会收到一个错误消息,说我向字典传递了错误的模型,并且它没有指定哪个局部视图导致了问题。每个分部都有不同的模型类型,一个IEnumerable称为产品,另一个IEnumerable称为服务,发票分部视图有一个viewmodel称为InvoiceViewModel

有人能解释一下如何使这项工作成功吗?顺便说一下,我对Razor和MVC有点在行

更新

我收到以下错误消息:


传递到字典中的模型项类型为“ProOptiInteractive.ViewModels.InvoiceViewModel”,但此字典需要类型为“ProOptiInteractive.ViewModels.InvoicePageViewModel”的模型项。

您可以将模型传递到局部视图,如下所示:

@Html.Partial("Invoice", Model.InvoiceViewModel) 
@model InvoiceViewModel

...
或者类似的东西

主视图的模型:

class InvoicePageViewModel {
...
    public InvoiceViewModel InvoiceViewModel { get; set; }
    public IEnumerable<Service> Services { get; set; }
    public IEnumerable<Product> Products { get; set; }
...
}

错误是因为您已将
发票
部分视图设置为具有类型为
发票页面视图模型
的模型,但您正在向其传递类型为
发票视图模型
的模型

将您的
InvoiceViewModel
属性更新为类型
InvoicePageViewModel
,或者将
Invoice
视图更改为使用类型
InvoiceViewModel

错误在
@Html.Partial(“发票”,model.InvoiceViewModel)

您的视图
发票
正在接受类型的模型
InvoicePageViewModel
并且您正在传递
InvoiceViewModel

将代码更改为
@Html.Partial(“发票”,型号)

或修改
Invoice
视图以接受
InvoiceViewModel
as


@model prooptioninteractive.ViewModels.InvoiceViewModel
Invoice.cshtml
可能以以下内容开头:

@model ProOptInteractive.ViewModels.InvoicePageViewModel
替换为:

@model ProOptInteractive.ViewModels.InvoiceViewModel

我发现了错误。这取决于控制器方法。当我调用索引视图(对应于ActionResult索引方法)时,我将viewmodel InvoiceViewModel返回到索引页面,即使它是强类型的InvoicePageViewModel。我把它改成了这样,这很有效:

public ActionResult Index()
{

    var invoice = InvoiceLogic.GetInvoice(this.HttpContext);

    // Set up our ViewModel
    var pageViewModel = new InvoicePageViewModel
    {
        Products = proent.Products.ToList(),
        Services = proent.Services.ToList(),
        InvoiceViewModel = new InvoiceViewModel
    {

        InvoiceItems = invoice.GetInvoiceItems(),
        Clients = proent.Clients.ToList(),
        InvoiceTotal = invoice.GetTotal()
    }
};
    // Return the view
    return View(pageViewModel);
}

谢谢大家的帮助和建议。

你能说得更具体些吗?是否可以发布错误消息?传递到字典中的模型项的类型为“ProOptiInteractive.ViewModels.InvoiceViewModel”,但此字典需要类型为“System.Collections.Generic.IEnumerable”“1[ProOptiInteractive.Models.Service]”的模型项。请尝试异步加载部分视图。若您得到错误,您可以添加更多参数来检查您从action方法传递了什么?理想情况下,它应该是InvoicePageViewModel。还要检查Invoice.cshtml中指定的模型,该模型应为InvoiceViewModel,这将要求我在一个视图中实例化三个不同的模型,这是不可能的。您可以创建一个模型,将所有三个部分模型放入其中,并像模型一样传递。产品或其他我更新了问题,我想我已经在做一些类似于你建议的事情了。是的,几乎可以肯定发票部分的模型声明中有一个o型。现在完全一样,Invoice.cshtml有一个名为InvoiceViewModel的模型声明。我查过了。因此我感到困惑。