分组数据模型和嵌套的foreach循环

时间:2014-06-11 19:20:59

标签: c# asp.net-mvc asp.net-mvc-4 razor foreach

我正在研究我的第一个复杂的MVC项目。我有一个现有的数据库,我使用Entity Framework 4.0进行建模。该项目基本上是一种调查工具。我的viewmodel中有8个表,每个表都有一些主要调查视图中需要的属性。这些基本上是问卷,部分,问题和可能的答案(以下拉列表的形式)加上中间连接表。

public class MyQuestionModel
{
  public Questionaire Questionaire { get; set; }
  public QuestionaireSection QuestionaireSection { get; set; }
  public Section Section { get; set; }
  public SectionQuestion SectionQuestion { get; set; }
  public Question Question { get; set; }
  public QuestionType QuestionType { get; set; }
  public QuestionAnswerListCode QuestionAnswerListCode { get; set; }
  public AnswerListCode AnswerListCode { get; set; }
}

我的ViewMode MyQuestionModel加载如下:

public ActionResult Index()
{
  var viewModel =
    from qa in db.Questionaires
    join qas in db.QuestionaireSections on qa.QuestionaireKey equals qas.QuestionaireKey
    join s in db.Sections on qas.SectionKey equals s.SectionKey
    join sq in db.SectionQuestions on s.SectionKey equals sq.SectionKey
    join q in db.Questions on sq.QuestionKey equals q.QuestionKey
    join qtc in db.QuestionTypes on q.QuestionTypeKey equals qtc.QuestionTypeKey
    join qddl in db.QuestionAnswerListCodes on q.QuestionKey equals qddl.QuestionKey
    join ddl in db.AnswerListCodes on qddl.AnswerListCodeKey equals ddl.AnswerListCodeKey
    where qa.QuestionaireName.Equals("TAD")
    select new MyQuestionModel
    {
      Questionaire = qa,
      QuestionaireSection = qas,
      Section = s,
      SectionQuestion = sq,
      Question = q,
      QuestionType = qtc,
      QuestionAnswerListCode = qddl,
      AnswerListCode = ddl
    };

  return View(viewModel);

  //var viewModel = new List<MyQuestionModel>();
  //return View(viewModel);

}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Index(IEnumerable<MyQuestionModel> model)
{
  if (ModelState.IsValid )
  {
    // PROCESS THE POSTED DATA HERE

    return RedirectToAction("Index", "MyQuestion");
  }

  // If we got this far, something failed, redisplay form
  ModelState.AddModelError("", "error message");
  return View(model);
}

通过使用嵌套的foreach和GroupBy和OrderBy Linq语句迭代结果来构造视图时,不会给我一个带有结果的模型,我可以回发给控制器。我尝试使用for循环而不是foreach但是分组问题导致我出现问题。如果我尝试离散地加载ViewModel而没有分组,并且只有每个表的不同数据,我最终会遇到问题,为每个表和复合ViewModel获取正确的类型。我猜这三种方法中的一种是可行的,但是我不知道通过哪种方式来悬挂我的帽子并通过让它工作来研磨。我在视图中使用嵌套的foreach循环获得了我需要的(See Image),但我认为我在进程中打破了模型,因为当我查看控制器中的已发布模型时它是null。也许我的foreach陈述没有正确构建。我不禁想到有一种更为精细的方法。最后我认为使用其他部分视图的编辑器模板可能是最好的,但我需要一个原型工作,我可以稍后进行优化。

@model IEnumerable<eValuate_Prototype_07.Models.MyQuestionModel>

@{
  ViewBag.Title = "Index";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()

<table>  

  @foreach (var group in (Model.OrderBy(x => x.QuestionaireSection.DefaultSequence).GroupBy(item => item.Section.SectionName)))
  {
    <tr>
      <th colspan="3">@group.Key</th>
    </tr>
    foreach (var item in group.OrderBy(x => x.SectionQuestion.DefaultSequence).GroupBy(subItem => subItem.Question.Question1).Distinct()) {
    <tr>
      <td>&nbsp;</td>
      <td>@item.Key</td>
      <td>
        <select id="ddlAnswerListCode"> 
          <option value="@Guid.Empty"></option>
          @foreach (var ans in item.OrderBy(x => x.QuestionAnswerListCode.DefaultSequence))
          {
            <option value="@ans.AnswerListCode.AnswerListCodeKey">@ans.AnswerListCode.AnswerListCodeName</option>
          }
        </select>
      </td>
    </tr>
    }
  }
</table>
<p>
  <input type="submit" value="Save" />
</p>  
}

我的问题:  哪个是首选的foreach,for循环,或者是离散地加载视图模型然后使用foreach?  我使用foreach打破模型发布的方式是什么?

0 个答案:

没有答案