2012-09-21 2 views
4

EF로 ASP.NET MVC 4 인터넷 응용 프로그램이 있습니다.MVC 4의 마스터 세부 정보 스타일보기

내 모델 : 주문 및 OrderDetails의 : Orders and OrderDetails

내보기 : Order View

Q : 어떻게 내 주문보기에서 내 OrderDetails의 기록을 표시 할 수 있습니까? (함께 또는 webgrid 또는 테이블?; JS없이?)

Create.cshtml

<div class="editor-label"> 
     @Html.LabelFor(model => model.OrderDate) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.OrderDate) 
     @Html.ValidationMessageFor(model => model.OrderDate) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.OrderNo) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.OrderNo) 
     @Html.ValidationMessageFor(model => model.OrderNo) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Total) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Total) 
     @Html.ValidationMessageFor(model => model.Total) 
    </div> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.Shipping) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Shipping) 
     @Html.ValidationMessageFor(model => model.Shipping) 
    </div> 
    @*OrderDetails Part - Works only on the Edit part*@ 
    <table> 
     <tr> 
      <th>Product</th> 
      <th>Unit Price</th> 
      <th>Quantity</th> 
      <th>Discount</th> 
     </tr> 
    @foreach (OrderDetails item in Model.OrderDetails.OrderBy(d=>d.Products.ProductName)) 
    { 
     <tr> 
      <td>@item.Products.ProductName</td> 
      <td>@item.UnitPrice</td> 
      <td>@item.Quantity</td> 
      <td>@item.Discount</td> 
      <td> 
       @Html.ActionLink("Edit", "Edit", "OrderDetails", new { id = item.OrderDetailId }, new { }) | 
       @Html.ActionLink("Details", "Details", "OrderDetails", new { id = item.OrderDetailId }, new { }) | 
       @Html.ActionLink("Delete", "Delete", "OrderDetails", new { id = item.OrderDetailId }, new { }) 
      </td> 
     </tr> 
    } 
    </table> 

    <p> 
     <input type="submit" value="Create" /> 
    </p> 
+0

[무엇을 시도해 봤습니까?] (http://mattgemmell.com/2008/12/08/what-have-you-tried/) 코드의 어느 부분에 좀 더 구체적으로 도움이 필요합니까? –

+0

주문 세부 정보 레코드를 주문 생성보기에 표시하는 중 – Misi

+0

부분보기가 더 쉬워 집니까? – Misi

답변

3

이 작업을 실현하려 viewmodels를 사용한다. 비즈니스 모델을 뷰에 직접 노출해서는 안됩니다. 대신 뷰 모델을 사용하여 다른 수준의 추상화를 제공하십시오.

따라서 모델을 전혀 변경하지 마십시오. 보기에 대한보기 모델을 정의하십시오. 나는 모든 필드를 viewmodel에 포함시키지 않을 것이다.

/* Viewmodel for your Order/Create page */ 
public class OrderCreate 
{ 
    /* Attributes for your order */ 
    public string OrderDate   { get; set; } 
    public string OrderNo   { get; set; } 
    public string Shipping   { get; set; } 
    .... 
    .... 

    /* List that will contain all details */ 
    public IList<ProductDetail> ProductDetails  { get; set; } 

} 

/* Viewmodel for each of the products */ 
public class ProductDetail 
{ 
    public string Product   { get; set; } 
    public string UnitPrice   { get; set; } 
    public string Quantity   { get; set; } 
    .... 
    .... 
} 

는 이제 GET 동작에보기 대신 비즈니스 모델이 뷰 모델을 반환합니다. 이 뷰 모델로
// 
// GET: /Order/Create 

public ActionResult Index() 
{ 
    /* New viewmodel for your view */ 
    var viewModel = new OrderCreate(); 
    viewModel.ProductDetails = new List<ProductDetail>(); 

    /* Assuming the number of products is static */ 
    for(int i = 0; i < NUMBER_OF_PRODUCTS; i++) 
    { 
     viewModel.ProductDetails.Add(new ProductDetail()); 
    } 

    return View(viewModel); 
} 

당신은 지금보기에 채워진 값에 액세스 할 수 있습니다. 작업을 게시하기 위해보기를 게시 할 때 viewmodel의 데이터를 사용하여 비즈니스 모델을 만듭니다. 당신은 실제 모델에 viewmodels 매핑 지루 될 경우

// 
// POST: /Order/Create 

[HttpPost] 
public ActionResult Index(OrderCreate viewModel) 
{ 
    if(ModelState.IsValid)) 
    { 
     var model = new Order(); 

     //TODO: Populate model through viewmodel, loop viewModel.ProductDetails 

     return RedirectToAction("Index"); 
    } 

    // Model is not valid 
    return View(viewMode); 
} 

어떤 조언, AutoMapper

는 희망이 도움이보십시오.

관련 문제