radiobuttonfor 및 checkboxFor를 사용하여 단일 또는 복수 응답 퀴즈를 만들고 싶지만 작동시키지 못합니다. 내가 보는 모든 예제의 문제점은 Question 모델도 SelectedAnswer를 등록한다는 것입니다. 그러나 제 경우에는 일부 질문에 여러 답변이 포함될 수 있으므로 각 가능한 대답을 선택해야합니다. 따라서 isSelected 속성은 Answer 모델 내에 직접 있습니다.복수 응답 그룹 및 답변 asp.net MVC
따라서 하나의 답변이있는 질문의 경우 RadioButtonFor (m => m [question] .Answers [answerToDisplayId] .IsSelected)를 사용하여 모델을 만들려고 할 때 모든 대답은 자체 그룹에 속하므로 선택하지 않은 경우 내가 현재 가지고있는 질문 (기본적으로는 checkBoxFor처럼 행동)
에서 다른 답변을 확인 : 질문 모델을
public enum questionfield
{
Chaser, Beater, Seeker, Contact, Process, Other
};
public enum QuestionDifficulty
{
Basic, Advanced
};
public enum AnswerType
{
SingleAnswer, MultipleAnswer
}
public class Question
{
public int Id { get; set; }
[Required(ErrorMessage = "Question name not valid")]
public string Name { get; set; }
[Required]
public QuestionField Field { get; set; }
[Required]
public QuestionDifficulty Difficulty { get; set; }
[Required]
public bool IsVideo { get; set; }
public string VideoURL { get; set; }
[Required]
public string QuestionText { get; set; }
[Required]
public AnswerType AnswerType { get; set; }
[Required]
public List<Answer> Answers { get; set; }
[Required]
public String AnswerExplanation { get; set; }
대답 모델 :
public class Answer
{
public int Id { get; set; }
public String Answertext { get; set; }
public Boolean IsTrue { get; set; }
public Boolean IsSelected { get; set; }
}
뷰 :
<div>
<!-- For each Question, display a new div with the Title, the question code, the question text, the video if there is one, then the possible answers depending on the type of answers-->
@using(Html.BeginForm("QuizzResult", "Home"))
{
for(int i = 0; i < Model.Count; i++)
{
<div class="QuizzQuestion">
<div class="QuizzQuestionTitle">@Model[i].Id : @Model[i].Name</div> @Html.HiddenFor(m => Model[i].Id)
<div class="QuizzQuestiontext">@Model[i].QuestionText</div>
@if(@Model[i].IsVideo)
{
<div class="QuizzQuestionVideoContainer">
<iframe class="QuizzQuestionVideo" id="ytplayer" type="text/html"
src="@Model[i].VideoURL"
frameborder="0"></iframe>
</div>
}
<div class="RadioButtonAnswers">
@if (@Model[i].AnswerType == QRefTrain3.Models.AnswerType.SingleAnswer)
{
for (int j = 0; j < Model[i].Answers.Count; j++)
{
@Model[i].Answers[j].Answertext @Html.RadioButtonFor(m => m[i].Answers[j].IsSelected, true)
@Html.HiddenFor(m => Model[i].Answers[j].IsTrue)
}
}
</div>
</div>
}
<input type="submit" value="Validate Answers"/>
}
</div>
컨트롤러 :
[HttpPost]
public ActionResult QuizzResult(List<Question> answers)
{
foreach(Question a in answers)
{
var b = Request.Form[a.Id.ToString()];
}
Result result = new Result();
foreach (Question q in answers)
{
result.QuestionsAskedIds.Add(q.Id);
if (Question.IsGoodAnswer(q))
{
result.GoodAnswersIds.Add(q.Id);
}
}
if (User.Identity.IsAuthenticated)
{
result.User = Dal.Instance.GetUserByName(HttpContext.User.Identity.Name);
Dal.Instance.CreateResult(result);
}
return View("QuizResult", result);
}
어떻게이 작업을 수행 할 수있는 좋은 방법이 될 것입니다? 고맙습니다!