I am currently having an issue with trying to post information from multiple choice answer options via radio buttons using razor pages and ASP.net Core. I've found some info for MVC but can't quite fix my issue with it.
I have a Razor page that displays a list of questions and, within each question its possible answer options. This is my form below:
<form method="post">
@for (int i = 0; i < Model.Questions.Count(); i++)
{
<h4>@Model.Questions.ElementAt(i).QuestionBody</h4>
<input type="hidden" asp-for="Question.QuestionBody" />
@foreach (var answer in Model.Answers)
{
var groupID = "question" + i;
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="radio" name="@groupID" />
<label>answer.AnswerBody</label>
</div>
</div>
</div>
}
}
<button type="submit" class="btn btn-primary">Save</button>
</form>
From info I've read I think I need to remove the name attribute but currently it is what is grouping my answers to the correct questions and allowing me to select multiple answers per page.
The Page Model for the ApTest razor page which contains the form is
public class ApTestModel : PageModel
{
private readonly IConfiguration config;
public IApTestData ApTestData { get; set; }
[BindProperty]
public IEnumerable<Question> Questions { get; set; }
[BindProperty]
public Question Question { get; set; }
public int QuestionID { get; set; }
[BindProperty]
public List<Answer> Answers { get; set; }
[BindProperty]
public Answer Answer { get; set; }
[BindProperty]
public TestAttempt TestAttempt { get; set; }
public ApTestModel(IConfiguration config, IApTestData ApTestData)
{
this.config = config;
this.ApTestData = ApTestData;
}
public void OnGet()
{
Questions = ApTestData.GetQuestionsAndAnswers(QuestionID);
}
public void OnPost()
{
AppUser appUser = new AppUser
{
Email = User.Identity.Name,
};
ApTestData.SaveApTestAttempt(Answers, appUser);
}
}
}
The SaveApTestAttempt method is here:
public void SaveApTestAttempt(List<Answer> answers, AppUser appUser)
{
var tempAppUser = db.Users.SingleOrDefault(user => user.Email == appUser.Email);
foreach (var answer in answers)
{
TestAttempt testAttempt = new TestAttempt
{
SelctedAnswer = answer,
Applicant = appUser,
ApplicantID = tempAppUser.Id
};
db.TestAttempts.Add(testAttempt);
}
}
and the entities involved are Question, Answer, and TestAttempt (All seperate classes but combined here for easy viewing):
public class Question
{
public int ID { get; set; }
public string QuestionBody { get; set; }
public List<Answer> Answers { get; set; }
public QuestionTypeID QuestionTypeID { get; set; }
public QuestionType QuestionType { get; set; }
public Question()
{
Answers = new List<Answer>();
}
}
public class Answer
{
public int ID { get; set; }
public string AnswerBody { get; set; }
public bool IsCorrect { get; set; }
public Question Question { get; set; }
public int QuestionID { get; set; }
}
public class TestAttempt
{
[Key]
public int ID { get; set; }
public AppUser Applicant { get; set; }
public string ApplicantID { get; set; }
public Answer SelctedAnswer { get; set; }
public int AnswerID { get; set; }
}
I know I should be binding and using asp-for within my form but I have tried that as well as removing the name attribute and adding a value but I don't think I am ever adding the correct thing. I was using something like Model.Questions.ElementAt(i).Answers[j].AnswerID
Still fairly new to the ASP.Net Core world and working on my dissertation project for uni, Any help would be greatly appreciated!!
