MVC 4评级系统

时间:2015-05-09 13:40:07

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

抱歉,我对MVC4很新。 我正在建立一个mvc4评级系统。

首先,我创建了一个输入评级问题字段的视图,并将信息存储到评级db中,如下所示:

Rating Question Input
==============================
Question : [ What Fruit You Like? ] 
Sub Question : Apple ; Orange ; Grapes
Number of Radio Button : [3]
Rating Header : Hate ; Like ; Very Like

我的Ratingdb字段:

public class RatingInput
    {
        public int Id { get; set; }
        public int QuestionId { get; set; }
        public string Question { get; set; }
        public string Subquestion { get; set; }
        public int NumberRadioButton { get; set; }
        public string ratingheader { get; set; }
    }

db:中的数据如何?

Question             | Subquestion         | NumberRadioButton | ratingheader
What Fruit you Like? | Apple;Orange;Grapes |         3         | Hate;Like;Very Like

如何在表格中为问题和单选按钮生成单独的行? 说明输出如下:

1. What Fruit You Like ?

                  Hate     Like      Very Like
Apple             []        []          []
Orange            []        []          []
Grapes            []        []          []

我的评分输出页面代码示例:

 @foreach (var item in Model)
    {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.Question)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.SubQuestion) *Need Help
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NumberRadioButton) *Need Help
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ratingheader) *Need Help
            </td>

        </tr>
    }

1 个答案:

答案 0 :(得分:2)

您需要创建视图模型来表示您想要显示/编辑的内容,这可能看起来像

public class SubQuestionVM
{
  public int ID { get; set; }
  public string Text { get; set; }
  public string Rating { get; set; }
}
public class QuestionVM
{
  public int ID { get; set; }
  public string Text { get; set; }
  public List<SubQuestionVM> SubQuestions { get; set; }
}

然后在控制器中,初始化一个新的QuestionVM集合并将其传递给视图

public ActionResult Edit()
{
  List<QuestionVM> model = new List<QuestionVM>();
  // Populate it from the database, but for testing
  model.Add(new QuestionVM()
  {
    ID = 1,
    Text = "What Fruit You Like ?",
    SubQuestions = new List<SubQuestionVM>()
    {
      new SubQuestionVM() { ID = 1, Text = "Apple" }, // include Rating="Hate" (or some value) if you want a default radio button selected
      new SubQuestionVM() { ID = 2, Text = "Orange" },
      new SubQuestionVM() { ID = 3, Text = "Grapes" }
    }
  });
  return View(model);
}

并在视图中

@model List<yourAssembly.QuestionVM>
@using (Html.BeginForm())
{
  for(int i = 0; i < Model.Count; i++)
  {
    @Html.HiddenFor(m => m[i].ID)
    @Html.DisplayFor(m => m[i].Text)
    for(int j = 0; j < Model[i].SubQuestions.Count; j++)
    {
      @Html.HiddenFor(m => m[i].SubQuestions[j].ID)
      @Html.DisplayFor(m => m[i].SubQuestions[j].Text)
      var id = string.Format("Hate-{0}", Model[i].SubQuestions[j].ID);
      @Html.RadioButtonFor(m => m[i].SubQuestions[j].Rating, "Hate", new { id = id })
      <label for="@id">Hate</label>
      id = string.Format("Like-{0}", Model[i].SubQuestions[j].ID);
      @Html.RadioButtonFor(m => m[i].SubQuestions[j].Rating, "Like", new { id = id })
      <label for="@id">Like</label>
      // ditto for 'Very Like'
    }
  }
  <input type="submit" />
}

并发回

public ActionResult Edit(List<QuestionVM> model)

注意:此处显示的内容仅基于您在问题中包含的内容。您应该设置Rating和枚举和/或添加[Required]验证属性,以确保为每个子问题选择评级。