简体   繁体   English

MVC中同一页面上有多个ViewModel

[英]more than one ViewModel on same page in MVC

I do have a form where in I have three partial Views and one Grid so for all these 4 there are 4 Different ViewModel is being used.Now as per My understanding there would be one more ViewModel which includes all the 4 ViewModel and out of which one would be IEnumarable that would be used by Grid. 我确实有一种形式,其中我有三个部分视图和一个网格,因此对于这四个视图,都使用了4种不同的ViewModel。根据我的理解,现在将有另外一个ViewModel,其中包括所有4个ViewModel,其中不包含一个将是IEnumarable,将由Grid使用。

My Code goes as follows: 我的代码如下:

    @model IEnumerable<Invoice.Models.ViewModels.Setup.AccountViewModel>

    @{
        ViewBag.Title = "Account";
        Layout = "~/Views/Shared/_LayoutGrid.cshtml";
    }



        @(Html.Kendo().Grid(Model)
            .Name("Account")
            .Columns(columns =>
            {

                columns.Bound(p => p.AccountType).Title("Account Type").Width(250);

                columns.Bound(p => p.AccountName).Title("Account Name").Width(300);

                columns.Bound(p => p.Currency).Title("Currency").Width(120);

                columns.Bound(p => p.Status).ClientTemplate("#=data.Status#").Width(150);

            })
            .ToolBar(toolbar =>
             {
                 toolbar.Template(@<text>
                    <div class="row-fluid">
                        <div class="span1">
                            <div class="toolbar" style="height:25px;">
                                <ul id="menu" style="width:38px; margin-left:22px;" class="span6">
                                    <li style="width:36px;">
                                        <i class="fa fa-plus"></i>
                                        <ul>
                                            @foreach (var account in @ViewBag.AccountType)
                                            {
                                                <li style="width:100px;" class="openid">
                                                    @if(@account.Value=="Bank")
                                                    {
                                                    <label id="Bank1">@account.Value</label>
                                                    }
                                                    @if(@account.Value=="CreditCard")
                                                    {
                                                    <label id="Credit">@account.Value</label>
                                                    }
                                                    @if(@account.Value=="Cash")
                                                    {
                                                    <label id="Cash1">@account.Value</label>
                                                    }
                                                </li>

                                            }
                                        </ul>
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </text>);
             })


            <div id="Bank">
            @Html.Partial("_BankDetails")
        </div>

        <div id="Cash">
            @Html.Partial("_CashDetails")
        </div>

        <div id="Credit">
            @Html.Partial("_CreditCardDetails")
    </div>

Here as You can see I am Using AccountViewModel that is being used for displaying the Grid Only but there are 3 More ViewModel for Submiting these Partial View. 如您所见,这里我正在使用AccountViewModel,该帐户仅用于显示网格,但是还有3个用于提交这些局部视图的ViewModel。

You could create a view models which combines the 4 view models you need: 您可以创建一个视图模型,将所需的4个视图模型结合在一起:

public class FullAcountModel
{
    public IEnumerable<AccountViewModel> Accounts { get; set; }

    public CashDetails CashDetails { get; set; }

    public BankDetails BankDetails { get; set; }

    public CreditCardDetails CreditCardDetails { get; set; }
}

With this model you can create the grid like this: 使用此模型,您可以像这样创建网格:

@(Html.Kendo().Grid(Model.Accounts)

And the partials like this: 像这样的部分:

@Html.Partial("_BankDetails", Model.BankDetails)

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

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