1 回答
TA贡献1777条经验 获得超3个赞
请参阅下面的我的解决方案。我为问题、子问题和选项创建了 3 个表。选项与父问题相关联(假设所有问题的选项都相同)。问题和子问题显示在文本框中(以使其可编辑)。您可以修改它以适合您的需要。希望这可以帮助。
这是我的观点:
@model List<QuestionsTest.Models.QuestionModel>
<form method="post">
<table class="table">
@for (int i = 0; i < Model.Count; i++)
{
<tr>
<td colspan="2">
@Html.HiddenFor(model => Model[i].Id, new { @class = "form-control" })
<b> @Html.TextBoxFor(model => Model[i].Question, new { @class = "form-
control" })</b>
</td>
</tr>
for (int j = 0; j < Model[i].SubQuestions.Count; j++)
{
<tr>
<td>
@Html.HiddenFor(model => Model[i].SubQuestions[j].Id, new { @class =
"form-control" })
@Html.HiddenFor(model => Model[i].SubQuestions[j].ParentQuestionId, new {
@class = "form-control" })
@Html.TextBoxFor(model => Model[i].SubQuestions[j].SubQuestion1, new {
@class = "form-control" })
</td>
<td>
@for (int k = 0; k < Model[i].Options.Count; k++)
{
@Html.RadioButtonFor(model => Model[i].Options[k].QuestionOption,
Model[i].Options[k].QuestionOption)@Model[i].Options[k].QuestionOption
}
</td>
</tr>
}
}
</table>
<input class="btn-block btn-success" type="submit" value="Update" />
这是我的控制器:
public ActionResult Questions()
{
var questions = _laraTestEntities.Questions.ToList();
var questionModel = new List<QuestionModel>();
questions.ForEach(q =>
{
var subQuestions = _laraTestEntities.SubQuestions.Where(s => s.ParentQuestionId == q.Id).ToList();
var options = _laraTestEntities.Options.ToList();
var model = new QuestionModel
{
Id = q.Id,
Question = q.Question1,
SubQuestions = subQuestions,
Options = options
};
questionModel.Add(model);
});
return View(questionModel);
}
[HttpPost]
public ActionResult Questions(List<QuestionModel> Model)
{
Model.ForEach(q =>
{
var questionDetail = _laraTestEntities.Questions.Find(q.Id);
if (questionDetail != null)
{
questionDetail.Question1 = q.Question;
q.SubQuestions.ForEach(s =>
{
var subQuestionDetail = _laraTestEntities.SubQuestions.Find(s.Id);
if (subQuestionDetail != null)
{
subQuestionDetail.SubQuestion1 = s.SubQuestion1;
}
});
_laraTestEntities.SaveChanges();
}
});
return View(Model);
}
模型:
public class QuestionModel
{
public int Id { get; set; }
public string Question { get; set; }
public List<SubQuestion> SubQuestions { get; set; }
public List<Option> Options { get; set; }
}
public class SubQuestion
{
public int Id { get; set; }
public int ParentQuestionId { get; set; }
public string SubQuestion1 { get; set; }
public virtual QuestionCategory QuestionCategory { get; set; }
}
public class Option
{
public int Id { get; set; }
public string OptionTitle { get; set; }
}
- 1 回答
- 0 关注
- 155 浏览
添加回答
举报