如何用单个视图绑定多个模型?

时间:2013-07-17 09:15:47

标签: asp.net-mvc asp.net-mvc-4

伙计们我试图在单一视图中使用多个模型。

但是我找不到解决方法如何实现它。

我想在Single过去使用One View数据,在另一部分使用另一个..

我使用了以下代码..

这是一个视图

@Html.Partial("Sidebar")

这是另一种观点

    <!-- start content -->
    <div id="content">
        <div class="post">
            <h1 class="title">
                Add New Post
            </h1>

<p></p>
            <div class="entry">
                @using (Html.BeginForm())
                {
                    <div>
                        <div style="display:inline;">
                            <div style="display: inline; float: left;">
                                @Html.Label("lblcategory", "Category", new { style = "Width:100px; float: left;" })
                                <div style="width: 150px; height: 60px; overflow-y: scroll; display: inline; float: left;">
                                    @for (int i = 0; i < (int)TempData["Rows"]; i++)
                                    {
                                        for (int j = 0; j < (int)TempData["Cols"]; j++)
                                        {
                                            <input id="Checkbox + @i" name = "Category" type="checkbox" style="width:50px;" value="@TempData["[" + i.ToString() + "][" + j.ToString() + "]"]"/>
                                            @TempData["[" + i.ToString() + "][" + j.ToString() + "]"]
                                        }
                                    }                                   
                                    @*@Html.LabelFor(model => model.CategoryName)*@
                                </div>
                                <div style="float:right;">
                                    <label id="lblcategoryrequired" style="color:Red">@Html.ValidationMessageFor(model => model.CategoryName)</label>
                                </div>
                            </div>
                        </div>
                        <div>
                            <p style="display: inline; float: left;">
                                @Html.Label("lblsubjet", "Subject", new { style = "Width:100px; float: left;" })
                                @*@Html.TextBox("txtsubject", "", new { style = "Width:700px;" })*@
                                @Html.TextBoxFor(model => model.PostSubject, new { style = "Width:400px; maxlength=400;" })
                                <label id="lblsubjectrequired" style="color:Red">@Html.ValidationMessageFor(model => model.PostSubject)</label>
                            </p>
                        </div>
                        <div>
                            <p style="display: inline; float: left;">
                                @Html.Label("lblcontent", "Content", new { style = "Width:100px; float: left; Vertical-align:top;" })
                                @*@Html.TextArea("txtcontent","", new { style = "Width:700px; height:200px; maxlength=700;" })*@
                                @Html.TextAreaFor(model => model.PostContent, new { style = "Width:400px; height:200px; maxlength=400;" })
                            </p>
                        </div>
                        <div>
                            <p style="display: inline; float: left;">
                                @Html.Label("lblblank", "a", new { style = "Width:100px; float: left; Color:#372412" })
                                <input type="submit" value="Add" id="btnadd" style="width: 100px;" class="button" />
                                &nbsp&nbsp&nbsp&nbsp
                                <a id="Cancel" href="~/Home/Home"> <input type="button" value="Cancel" id="btncancel" class="button" style="width: 100px;" /></a>
                            </p>
                        </div>
                    </div>
                    @Html.ValidationSummary(true)
                }
            </div>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

我不明白你的问题100%。但是,如果我理解它,那么我认为它不会像你需要的那样工作(我可能会弄错)。我建议您远离部分视图,然后传入一个view model,您可以使用它来填充这两个部分。视图模型用于表示视图上的数据。

我将为您提供一个基本示例,您可以在场景中修改和使用它。假设我们有一个客户,这个客户可以有一个或多个地址。因此,这两个模型的基本表示可能如下所示:

public class Customer
{
     public int Id { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }

     public IEnumerable<Address> Addresses { get; set; }
}

public class Address
{
     public int Id { get; set; }

     public string AddressLine1 { get; set; }

     public string AddressLine2 { get; set; }

     public string AddressLine3 { get; set; }
}

现在,在您的详细信息视图中,您希望显示客户的详细信息和该客户的地址。因此,我们在1个视图中显示了2个模型(客户和地址)。

public ActionResult Details(int id)
{
     Customer customer = customerRepository.GetById(id);

     if (customer != null)
     {
          customer.Addresses = addressRepository.GetAddressesForCustomer(customer.Id);
     }

     // The above 2 steps can be done in 1 repository call

     // Now populate your view model with the above details
     // This can also be 1 or 2 lines when you use something like Auto Mapper

     CustomerDetailsViewModel viewModel = new CustomerDetailsViewModel
     {
          viewModel.CustomerId = customer.Id,
          viewModel.CustomerFirstName = customer.FirstName,
          viewModel.CustomerLastName = customer.LastName,
          viewModel.CustomerAddresses = customer.Addresses
     };

     return View(viewModel);
}

您的观看模型:

public class CustomerDetailsViewModel
{
     public int CustomerId { get; set; }

     public string CustomerFirstName { get; set; }

     public string CustomerLastName { get; set; }

     public IEnumerable<Address> CustomerAddresses { get; set; }
}

现在您有2个不同模型填充的视图模型。现在,您只需要使用此视图模型来显示数据:

@model YourProject.ViewModels.Customers.CustomerDetailsViewModel

@Model.CustomerId<br />
@Model.CustomerFirstName<br />
@Model.CustomerLastName<br /><br />

@foreach (var address in @Model.CustomerAddresses)
{
     <div>
          @address.Id<br />
          @address.AddressLine1<br />
          @address.AddressLine2<br />
          @address.AddressLine3<br />
     </div>
}

我希望这会有所帮助。

答案 1 :(得分:0)

您应该使用表示渲染视图所需数据的视图模型。您可以直接在视图模型上公开模型(违反LoD),也可以将视图模型的调用委托给基础模型(违反DRY原则)。