1

I can't understand why I can't find anything that clearly explains how to do this with MVC. Seems like a pretty routine issue:

I have three tables:

PackageName: PackageNameID,PackageName
Food: FoodID,Food
PackageContent: PackageContentID, PackageNameID, FoodID, Qty

The application is supposed to describe packages of food. For example, a package named "A" might contain 4 onions and 3 peppers. Another package, "B", might contain 2 rolls and 2 onions.

Currently I have a custom view model ("PackageNameModel") that collects the data:

public ViewResult Index() {
    var viewModel =
        from pn in db.PackageNames
        from pc in db.PackageContents
            .Where(p => p.PackageNameID == pn.PackageNameID).DefaultIfEmpty()
        from f in db.Foods
            .Where(f => f.FoodID == pc.FoodID).DefaultIfEmpty()
        select new PackageFoodModel { PackageName = pn, PackageContent = pc, Food = f };
    return View( viewModel );      
}

This returns the data correctly, but what do I do with it so that the view is actually one that is useful for the application?

Using this in my view:

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.PackageName.PackageName1 ) , 
        @Html.DisplayFor(modelItem => item.Food.Food1)=@Html.DisplayFor(modelItem => item.PackageContent.Qty)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.PackageName.PackageNameID }) |
        @Html.ActionLink("Details", "Details", new { id = item.PackageName.PackageNameID }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.PackageName.PackageNameID })
    </td>
</tr>
}

I am able to display the following:

 - A , Onions=4  Edit | Details | Delete
 - A , Peppers=3  Edit | Details | Delete
 - B , Rolls=2  Edit | Details | Delete   
 - B , Onions=2  Edit | Details | Delete

This is not very useful. What I'm trying to do is display something like this:

 - A (Onions=4,Peppers=3)   Edit | Details | Delete
 - B (Rolls=2,Onions=2) Edit | Details | Delete

Eventually, the next page down after navigating to the "Edit" action would provide an editable name for the package as well as a table of all available foods and an adjacent quantity box so that the foods/quantities within each package may be updated.

Can anyone explain how this is done within the MVC framework? All the tutorials I have seen on the web/in books deal with data that is much simpler. Can anyone point me to some sample code / tutorials that deal with something like this?

Thanks in advance!

4

1 に答える 1

1

結果を集計する必要があります。

このようなビューモデルを作成します

public class PackageViewModel
{
  public int ID { set;get;}
  public string Name { set;get;}
  public List<FoodItem> FoodItems { set;get;}
  public PackageViewModel()
  {
     FoodItems=new List<FoodItem>();
  }
}
public class FoodItem
{
  public string FoodName { set;get;}
  public int Quantity { set;get;}
}

Getアクションでは、データアクセスレイヤーから取得したデータからデータを集約し、ViewModelリストに入力する必要があります

public ActionResult IndeX()
{
  List<PackageViewModel> listVM=new List<PackageViewModel>();

   //Get your data and do aggregation and fill in listVM

  return View(listVm)l
}

そして私たちの見解では、

@model List<PackageViewModel>

@foreach(var item in Model)
{
  <tr>
   <td>@item.Name</td>
   <td>
       foreach(var food in item.FoodItems)
       {
         @food.FoodName - @food.Quantity
       }
   </td>
   <td>
      @Html.ActionLink("Edit","Edit",new {@id=item.ID})
   </td>
  </tr>
}
于 2012-09-10T02:15:18.473 に答える