如何将List <model>传递给MVC 4中的控制器

时间:2015-12-11 11:00:00

标签: asp.net-mvc-4

我有2个模型:问题和答案如下,我想向View发送一个List模型,当提交表单时,我将List模型提交给控制器,但是在Action UpdateQuestion中只能获得问题列表但是答案清单不是。你能解释并告诉我如何在提交表格时获得每个问题的清单答案

public class Question
    {
        [Key]
        public int Id { get; set; }

        [ForeignKey("QuestionType")]
        public int QuestionTypeId { get; set; }
        public virtual QuestionType QuestionType { get; set; }

        [ForeignKey("Field")]
        public int FieldId { get; set; }
        public virtual Field Field { get; set; }

        public string Brief { get; set; }

        public bool IsGroup { get; set; }

        [ForeignKey("QuestionGroup")]
        public int? QuestionGroupId { get; set; }
        public virtual QuestionGroup QuestionGroup { get; set; }

        public int Priority { get; set; }

        public int Order { get; set; }

        public virtual ICollection<Answer> Answers { get; set; }
    }

public class Answer
    {
        [Key]
        public Int32 Id { get; set; }

        [Column(TypeName = "ntext")]
        [MaxLength]
        public string Content { get; set; }      

        [ForeignKey("Question")]      
        public int QuestionId { get; set; }
        public virtual Question Question { get; set; }      

        public float Mark { get; set; }

        public int Priority { get; set; }    
    }

我有控制器索引将问题列表传递给视图:

public ActionResult Index()
{
     ApplicationDbContext db = new ApplicationDbContext();
            var listQuestion = db.Questions.ToList();
return View(listQuestion);
}

[HttpPost]

        public ActionResult UpdateQuestion(string submit, List<Question> Questions)
        {
        ...

        return RedirectToAction("Index");
}

在视野中:

@model List<Question>
@{
    int i = 0;
    int j = 0;
}
@using (Html.BeginForm("UpdateQuestion", "TestRoom")) 
{ 
     <ul>
         @foreach(var question in Model)//Question
         {                                
              <li>
              @Html.Hidden("Questions["+i+"].Id", question.Id)
              @{i++;}
              @Html.Raw(question.Brief)
              <ul>
                   @foreach (var answers in question.Answers)
                   {                                            
                        <li>@Html.RadioButton("Questions["+i+"]_Answers["+j+"]",answers.Id)                                                
                                                @Html.Raw(answers.Content)

                                                @{j++;}
                                            </li>
                                        }
                                        @{j = 0;}
                                    </ul>
                                </li>
                            }

                        </ul>
                        <div class="aq-button-panel">
                            <button type="submit" value="Finish" name="submit"><i class="icon-pencil"></i>Submit</button>
                            <button type="submit" value="Back" name="submit">Go Next <i class="icon-arrow-left"></i></button>
                            <button type="submit" value="Next" name="submit">Go Back <i class="icon-arrow-right"></i></button>                        
                        </div>
                    }

1 个答案:

答案 0 :(得分:1)

您的代码存在多个问题。首先,您无法将单选按钮绑定到复杂对象(在您的情况下为Answer,因为单选按钮组仅回发单个值(在您的情况下,所选id的{​​{1}}值接下来你循环生成单选按钮组,试图将选定的答案绑定到第一个没有意义的答案(每次将Answer的值设置为j)。 model需要一个属性来绑定(比如)0

首先创建视图模型,表示您希望在视图中显示/编辑的内容(根据需要添加显示和验证属性)

int SelectedAnswer

在get方法中,获取数据模型并映射到视图模型并将public class AnswerVM { public int ID { get; set; } public string Content { get; set; } } public class QuestionVM { public int ID { get; set; } public string Brief { get; set; } public int SelectedAnswer { get; set; } public IEnumerable<AnswerVM> PossibleAnswers { get; set; } } 返回到视图。

接下来为IEnumerable<QuestionVM>EditorTemplate

类型创建QuestionVM
/Views/Shared/EditorTemplates/QuestionVM.cshtml

并在主视图中

@model QuestionVM
<li>
  @Html.HiddenFor(m => m.ID)
  @Html.DisplayFor(m => m.Brief)
  <ul>
    @foreach(var answer in Model.PossibleAnswers)
    {
      <li>
        <label>
          @Html.RadioButtonFor(m => m.SelectedAnswer, answer.ID, new { id = "" })
          <span>@answer.Content</span>
        </label>
      </li>
    }
  </ul>
</li>

并将POST方法更改为

@model IEnumerable<QuestionVM>
....
@Html.BeginForm(...))
{
  <ul>
    @Html.EditorFor(m => m) // this will generate the correct html for each question in the collection
  </ul>
  <div class="aq-button-panel">
    <button type="submit" ... />
    ...
  </div>
}

模型现在包含每个问题的ID以及每个问题的所选答案的ID。

请注意,如果由于[HttpPost] public ActionResult UpdateQuestion(string submit, IEnumerable<QuestionVM> model) 无效而需要返回视图,则需要重新填充每个问题的ModelState属性(不为每个PossibleAnswers生成每个属性的表单控件{{ 1}}在每个Answer中 - 也不应该这样)所以Question属性在提交表单时将是一个空集合。