如何按单独的表中定义的ItemOrder列出项目? MVC

时间:2015-12-10 18:56:54

标签: c# asp.net-mvc

我正在尝试按其他页面上的用户条目定义的项目订单排序。 我有一个项目表和一个链接到项目的单独的表项目订单。 我还为ItemCategories提供了一个单独的表。 这三个表都是相关的。

不同的调查具有特定项目,而不是可能需要以用户定义的顺序显示多个项目。

示例:

          Project 1                    |              Project 2 
ItemOrder   Item        ItemCategory   |ItemOrder   Item       ItemCategory 
1           Item a      CatA           |8           Item a     CatA
3           Item b      CatB           |7           Item b     CatB
4           Item c      CatC           |5           Item c     CatC
2           Item d      CatD           |4           Item d     CatD
5           Item e      CatE           |6           Item e     CatD
8           Item f      CatF           |1           Item f     CatF
6           Item g      CatG           |3           Item g     CatG
7           Item h      CatH           |2           Item h     CatH

我不知道如何根据ItemOrder将所有内容显示为项目和类别列表。

控制器:

public ActionResult Survey(int SurveyId) {
  var model = new SurveyViewModel(SurveyId);
  return View(model);
}

型号:

public class SurveyViewModel {
  public Survey Survey {get; set;}
  public int SurveyId {get; set;}
  public List<Project> Projects {get; set;}
  public List<OrderedItem> OrderedItems {get; set;}
}

public class OrderedItem {
  public int Id {get; set;}
  public int Order {get; set;}
  public string Category {get; set;}
  public string ItemText {get; set;}
}

public SurveyViewModel(int SurveyId) {
  using (var db = new SurveyContext()) {
   Projects = db.Projects.Include("Items")
                         .Include("Items.Category")
                         .Where(s => s.SurveyId == SurveyId).ToList();

   var Items = from s in Surveys
               from i in s.Project.Items
               from io in i.ItemOrders
               where s.Id == 1
               orderby io.ItemOrder
               select new OrderedItem {
               Id = i.Id,
               Category = i.Category.Category,
               Order = io.Order,
               ItemText = i.Text.Text
               }).Distinct();

//NOT SURE WHAT TO DO HERE TO POPULATE THE LIST TO PASS TO MY VIEW...
//THIS IS NULL...
   OrderedItems = OrderedItems.ToList(); //?????
  }
}

查看:

@model xxxx.Models.SurveyViewModel
  //Not sure what to do here either...
  @foreach (var item in Model.Survey.Project.Items) {
    @item.Text.Text //???
  }

请帮助,我想我大部分时间都在那里,但我遇到了麻烦。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

在视图模型中混合使用数据访问代码并不是一个好主意。让他们保持精益和平坦。

public class SurveyViewModel {
  public Survey Survey {get; set;}
  public int SurveyId {get; set;}
  public List<Project> Projects {get; set;}
  public List<OrderedItem> OrderedItems {get; set;}
}

public class OrderedItem {
  public int Id {get; set;}
  public int Order {get; set;}
  public string Category {get; set;}
  public string ItemText {get; set;}
}

在您的操作方法中,将数据加载到其中

public ActionResult Survey(int SurveyId) 
{
  var vm = new SurveyViewModel();

     using (var db = new SurveyContext())
     {
         vm.Projects = db.Projects.Include("Items")
                         .Include("Items.Category")
                         .Where(s => s.SurveyId == SurveyId).ToList();

         vm.OrderedItems  = from s in Surveys
                              from i in s.Project.Items
                              from io in i.ItemOrders
                              where s.Id == SurveyId
                              orderby io.ItemOrder
                           select new OrderedItem {
                                Id = i.Id,
                                Category = i.Category.Category,
                                Order = io.Order,
                                ItemText = i.Text.Text
                              }).Distinct().ToList();
     }
     return View(vm);
}

如果您想让控制器精简,您也可以将数据库访问代码移动到另一层。

你的剃刀视图将是

@model SurveyViewModel
<h2>Ordered Items </h2>
@foreach(var p in Model.OrderedItems )
{
  <p>@p.Category</p> 
  <p>@p.Order</p> 
}
<h2>Projects</h2>
@foreach(var proj in Model.Projects)
{
  <p>@proj.Name</p>      
}