0

みんな、単一のビューで複数のモデルを使用しようとしました。

しかし、それを実装する方法を見つけることができませんでした。

One ViewのデータをSingleの過去に、もう一方の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>
4

2 に答える 2

1

あなたの質問が100%理解できません。しかし、私がそれを理解したとしても、あなたが必要とするように機能するとは思いません (私は間違っているかもしれません)。部分的なビューから離れて、view model両方のセクションに入力するために使用できるものを渡すことをお勧めします。ビュー モデルは、ビューでデータを表すために存在します。

シナリオで変更して使用できる基本的なサンプルを提供します。顧客がいて、この顧客が 1 つまたは複数の住所を持つことができるとします。したがって、これら 2 つのモデルの基本的な表現は次のようになります。

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 つの異なるモデルから 1 つのビュー モデルが取り込まれました。ビューで行う必要があるのは、このビュー モデルを使用してデータを表示することだけです。

@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 に答える