0

伙计们,我曾尝试使用多个具有单一视图的模型。

但我找不到如何实现它的解决方案。

我想在过去的单一视图中使用一个视图数据,而在另一部分中使用另一个视图数据..

我使用了以下代码..

这是一种观点

@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>
4

2 回答 2

1

我不明白你的问题100%。但是,如果我要理解它,那么我认为它不会按照您需要的方式工作(我可能弄错了)。我建议您远离局部视图,只传递一个view model可用于填充两个部分的视图。视图模型用于在视图上表示您的数据。

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

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

现在,在您的详细信息视图中,您想要显示客户的详细信息和该客户的地址。所以我们有 2 个模型(客户和地址),您在 1 个视图上显示。

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

所以现在你有 1 个从 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>
}

我希望这有帮助。

于 2013-07-17T10:49:23.500 回答
0

您应该使用表示呈现视图所需数据的视图模型。您可以直接在视图模型上公开模型(违反LoD),或者将对视图模型的调用委托给底层模型(违反DRY原则)。

于 2013-07-17T09:18:25.323 回答