简体   繁体   English

在视图中渲染多个强类型的局部视图?

[英]Render multiple strongly typed partial views inside a view?

I have this Index.cshtml class: 我有这个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>

And this is my InvoicePageViewModel: 这是我的InvoicePageViewModel:

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

The issue is that whenever I load this view, I get an error saying that I have passed the wrong model to the dictionary, and it doesn't specify which one of the partial views is causing the problem. 问题在于,无论何时加载此视图,我都会收到错误消息,说我已将错误的模型传递给字典,并且未指定哪个部分视图导致问题。 Each partial has a different model type, one IEnumerable called Product, another IEnumerable called Service, and the Invoice partial view having a viewmodel called InvoiceViewModel. 每个部分都有不同的模型类型,一个名为Product的IEnumerable,一个名为Service的IEnumerable,以及一个名为InvoiceViewModel的视图模型的Invoice局部视图。

Can anybody explain how to go about making this work? 任何人都可以解释如何使这项工作? I'm a bit noob at Razor and MVC by the way. 顺便说一句,我在Razor和MVC上有点菜鸟。

UPDATE UPDATE

I get this error message: 我收到此错误消息:

The model item passed into the dictionary is of type 'ProOptInteractive.ViewModels.InvoiceViewModel', but this dictionary requires a model item of type 'ProOptInteractive.ViewModels.InvoicePageViewModel'. 传递到字典中的模型项的类型为“ProOptInteractive.ViewModels.InvoiceViewModel”,但此字典需要“ProOptInteractive.ViewModels.InvoicePageViewModel”类型的模型项。

The error is because you have set your Invoice partial view to have a model of type InvoicePageViewModel , yet you are passing it a model of type InvoiceViewModel . 该错误是因为您已将“ Invoice部分视图设置为具有类型为InvoicePageViewModel的模型,但您正在InvoicePageViewModel传递InvoiceViewModel类型的模型。

Either update your InvoiceViewModel property to be of type InvoicePageViewModel , or change the Invoice view to use a model of type InvoiceViewModel . 更新您的InvoiceViewModel属性为类型InvoicePageViewModel ,或更改Invoice使用类型的模型图InvoiceViewModel

Error is in line <div>@Html.Partial("Invoice", Model.InvoiceViewModel)</div> 错误在行<div>@Html.Partial("Invoice", Model.InvoiceViewModel)</div>

Your view Invoice is accepting model of type InvoicePageViewModel and you are passing InvoiceViewModel 您的视图Invoice正在接受InvoicePageViewModel类型的模型,并且您正在传递InvoiceViewModel

Change your code to <div>@Html.Partial("Invoice", Model)</div> 将您的代码更改为<div>@Html.Partial("Invoice", Model)</div>

or modify your Invoice view to accept InvoiceViewModel as 或修改您的Invoice视图以接受InvoiceViewModel

@model ProOptInteractive.ViewModels.InvoiceViewModel

Invoice.cshtml likely starts with: Invoice.cshtml可能以以下内容开头:

@model ProOptInteractive.ViewModels.InvoicePageViewModel

replace it with: 替换为:

@model ProOptInteractive.ViewModels.InvoiceViewModel

You can pass model to your partial view like this: 您可以将模型传递到部分视图,如下所示:

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

or something similar. 或类似的东西。

Model for main view: 主视图模型:

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

Then update your partial view to accept view model like this: 然后更新您的局部视图以接受这样的视图模型:

@model InvoiceViewModel

...

I discovered the error. 我发现了错误。 It was lying in the controller method. 它在于控制器方法。 When I called the Index view (which corresponds to the ActionResult Index method) I was returning the viewmodel InvoiceViewModel to the Index page, even though it was strongly typed to InvoicePageViewModel. 当我调用Index视图(对应于ActionResult Index方法)时,我将viewmodel InvoiceViewModel返回到Index页面,即使它强烈键入InvoicePageViewModel。 I changed it to this, which works: 我把它改成了这个,它有效:

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

Thanks to everyone for all the help and suggestions. 感谢大家的所有帮助和建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM