剃刀 post collection

Razor post collection

我 posting collection 有问题。我刚开始学习 MVC 和剃刀。 我正在编写一个小问卷应用程序,我正在做的就是填充问题列表并让用户回答问题和 post 答案。

我在模型 Questionaire 和 QuestionRepository 中有两个 class 控制器有两种方法,一种用于获取问题,另一种用于获取 post 数据,我在填充时遇到的问题用文本框形成我正在努力 post 支持 collection.

基本上我想要实现的是拉出问题列表,在 UI 上填充一个文本框,将问题列表和答案发送给控制器以处理一些逻辑。

如果有人能帮助我,我将不胜感激。

public class Questionnaire
{
    public string Title {
        get;
        set;
    }
    public IList<string> Questions {
        get;
        set;
    }
    public string Answer {
        get;
        set;
    }
}

public Questionnaire GetQuestionnaire() {
    return new Questionnaire {
    Title = "Geography Questions",
      Questions = new List<string>  
       {
          "What is the capital of Cuba?",
          "What is the capital of France?",
          "What is the capital of Poland?",
          "What is the capital of Germany?"
         }
        };
    }

    public int GetCustomerAnswer() {
        return 0;
    }
}

控制器

public ActionResult Index()
{
  var q = new QuestionRepository().GetQuestionnaire();  
  return View(q);
}

[HttpPost]
public ActionResult GetAnswer(QuestionRepository q) {
   return View();
}

查看

@model Question.Models.Questionnaire
<h2>Question List</h2>
@using(Html.BeginForm("GetAnswer","Home"))
{
  for(int i=0;i< Model.Questions.Count;i++)
  {
    @Html.Label("Question")
    <text>@Model.Questions[i]</text>
    @Html.TextBox(Q => Model.Questions[i])
    <br /> 
  }
  <input type="submit" name="submit" />
}

如果你想post回一些东西。在[HttpPost]动作中,您需要return一个模型。目前,您只有 return 一个 View(),没有要显示的模型数据。你应该这样做:

[HttpPost]
public ActionResult GetAnswer(QuestionRepository q) {
   var model = new Answer();
   model.answer = q.answer;
   return View(model);
}

您还需要创建模型答案。

    public class Answer
    {
         public string answer { get; set; }
    }

首先你的模型是错误的,没有定义问题和相关答案之间的任何关系(你只能为所有问题添加一个答案)。其次,您的视图将文本框绑定到每个问题,允许用户仅编辑问题而不添加答案。一个简单的例子可能看起来像

查看型号

public class QuestionVM
{
  public int ID { get; set; }
  public string Text { get; set; }
}

public class AnswerVM 
{
  public QuestionVM Question { get; set; }
  public int ID { get; set; }
  public string Text { get; set; }     
}

public class QuestionnaireVM
{
  public int ID { get; set; }
  public string Title { get; set; }
  public List<AnswerVM> Answers { get; set; }
}

控制器

public ActionResult Create(int ID)
{
  QuestionnaireVM model = new QuestionnaireVM();
  // populate the model from the database, but a hard coded example might be
  model.ID = 1;
  model.Title = "Questionaire 1";
  model.Answers = new List<AnswerVM>()
  {
    new AnswerVM()
    {
      Question = new QuestionVM() { ID = 1, Text = "Question 1" }
    },
    new AnswerVM()
    {
      Question = new QuestionVM() { ID = 2, Text = "Question 2" }
    }
  };
  return View(model);
}

[HttpPost]
public ActionResult Create(QuestionnaireVM model)
{
  // save you data and redirect
}

查看

@model QuestionnaireVM
@using(Html.BeginForm())
{
  @Html.DisplayFor(m => m.Title)
  for(int i = 0; i < Model.Answers.Count; i++)
  {
    @Html.HiddenFor(m => m.Answers[i].Question.ID)
    @Html.DisplayFor(m => m.Answers[i].Question.Text)
    @Html.TextBoxFor(m => m.Answers[i].Text)
  }
  <input type="submit" />
}

这将生成一个视图,显示每个问题以及关联的文本框以添加 answer.When 您 post 模型将包含问卷的 ID,以及包含每个问题 ID 的集合问题及其相关答案。

类似问题的其他几个例子 and here