如何在 ASP.NET MVC 中编辑分组行

我正在尝试编辑编辑中显示的选定行中的所有行,但也按以下格式分组:


注意:[] 代表复选框,在数据库中为 true 或 false


A. 您在服用以下药物时是否有这些症状?


            Yes No   Sometimes   Not Sure  After 21 days   Indifferent

(i) 扑热息痛 [] [] [] [] [] [] ...


(ii) Quninne [] [] [] [] [] [] ...


B. 用冷水和以下药物一起使用时,您的眼睛会闪烁吗?


            Yes No   Sometimes   Not Sure  After 21 days   Indifferent

(i) 维生素 C [] [] [] [] [] [] ...


(ii) Quninne [] [] [] [] [] [] ,,,


我的 POCO 设计是这样的:


public class QuestionCategory

{

  public int QuestionCategoryID {get; set;}

  public string Name {get; set;}

  ...

}


public class Question

{

  public int ID {get; set;}

  public string Name {get; set;}

  public int QuestionCategoryID  {get; set;}

  ...

}

在视图中


我可以按 QuestionCategoryID 对问题进行分组,现在的问题是如何列出所有问题并单击一个按钮进行编辑。


所以我决定制作一个我想要实现的线框。

http://img.mukewang.com/61a35db200015dcc06120578.jpg

青春有我
浏览 156回答 1
1回答

慕森王

请参阅下面的我的解决方案。我为问题、子问题和选项创建了 3 个表。选项与父问题相关联(假设所有问题的选项都相同)。问题和子问题显示在文本框中(以使其可编辑)。您可以修改它以适合您的需要。希望这可以帮助。这是我的观点:&nbsp;@model List<QuestionsTest.Models.QuestionModel>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <form method="post">&nbsp; &nbsp; &nbsp; <table class="table">&nbsp; &nbsp; &nbsp; &nbsp; @for (int i = 0; i < Model.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td colspan="2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.HiddenFor(model => Model[i].Id, new { @class = "form-control" })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>&nbsp; @Html.TextBoxFor(model => Model[i].Question, new { @class = "form-&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; control" })</b>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < Model[i].SubQuestions.Count; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.HiddenFor(model => Model[i].SubQuestions[j].Id, new { @class =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "form-control" })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.HiddenFor(model => Model[i].SubQuestions[j].ParentQuestionId, new {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @class = "form-control" })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.TextBoxFor(model => Model[i].SubQuestions[j].SubQuestion1, new {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @class = "form-control" })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @for (int k = 0; k < Model[i].Options.Count; k++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Html.RadioButtonFor(model => Model[i].Options[k].QuestionOption,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Model[i].Options[k].QuestionOption)@Model[i].Options[k].QuestionOption&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; </table>&nbsp; &nbsp; &nbsp; <input class="btn-block btn-success" type="submit" value="Update" />这是我的控制器:public ActionResult Questions(){&nbsp; var questions = _laraTestEntities.Questions.ToList();&nbsp; var questionModel = new List<QuestionModel>();&nbsp; questions.ForEach(q =>&nbsp; {&nbsp; &nbsp; var subQuestions = _laraTestEntities.SubQuestions.Where(s => s.ParentQuestionId == q.Id).ToList();&nbsp; &nbsp; var options = _laraTestEntities.Options.ToList();&nbsp; &nbsp; var model = new QuestionModel&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; Id = q.Id,&nbsp; &nbsp; &nbsp; Question = q.Question1,&nbsp; &nbsp; &nbsp; SubQuestions = subQuestions,&nbsp; &nbsp; &nbsp; Options = options&nbsp; &nbsp; };&nbsp; &nbsp; questionModel.Add(model);&nbsp; });&nbsp; return View(questionModel);}[HttpPost]public ActionResult Questions(List<QuestionModel> Model){&nbsp; Model.ForEach(q =>&nbsp; {&nbsp; &nbsp; var questionDetail = _laraTestEntities.Questions.Find(q.Id);&nbsp; &nbsp; if (questionDetail != null)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; questionDetail.Question1 = q.Question;&nbsp; &nbsp; &nbsp; q.SubQuestions.ForEach(s =>&nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var subQuestionDetail = _laraTestEntities.SubQuestions.Find(s.Id);&nbsp; &nbsp; &nbsp; &nbsp; if (subQuestionDetail != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subQuestionDetail.SubQuestion1 = s.SubQuestion1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; _laraTestEntities.SaveChanges();&nbsp; &nbsp; }&nbsp; });&nbsp; return View(Model);}模型:&nbsp; &nbsp; &nbsp;public class QuestionModel&nbsp; {&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string Question { get; set; }&nbsp; &nbsp; public List<SubQuestion> SubQuestions { get; set; }&nbsp; &nbsp; public List<Option> Options { get; set; }&nbsp; }public class SubQuestion&nbsp; {&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public int ParentQuestionId { get; set; }&nbsp; &nbsp; public string SubQuestion1 { get; set; }&nbsp; &nbsp; public virtual QuestionCategory QuestionCategory { get; set; }&nbsp; }&nbsp;public class Option&nbsp; {&nbsp; &nbsp; public int Id { get; set; }&nbsp; &nbsp; public string OptionTitle { get; set; }&nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP