So I am making a survey website where you can create survey forms and fill them after, my question is if I have a question where i have a collection of it's options with bool if the option is marked on filling or not, how do I set that value to the model when i click onto Submit form button In short words get all marked answers from the questions, pass them to the controller as a separate view model that contains only the ID of the question and the given answer
Here is the class of the question :
public class OptionsQuestionVM
{
public Guid Id { get; set; }
public Guid FormId { get; set; }
public bool IsAnswered { get; set; }
public bool IsDeleted { get; set; }
public string Name { get; set; }
public ICollection<OptionModelVM> Options { get; set; }
public bool IsRequired { get; set; }
public bool HasMultipleAnswers { get; set; }
}
Here is the class of the OptionModelVM:
public class OptionModelVM
{
public Guid Id { get; set; }
public string Text { get; set; }
public bool IsMarked { get; set; }
public OptionsQuestionVM Question { get; set; }
public Guid OptionsQuestionId { get; set; }
}
And here is my HTML Code for the checkbox that will be displayed for each question and it's available answers:
@foreach (var item in Model.OptionsQuestions)
{
if (item.HasMultipleAnswers == true)
{
<div class="element-checkbox">
<hr>
<h4>@item.Name</h4>
<div class="column column1">
@foreach (var option in item.Options)
{
<label>
<input type="checkbox" name="checkbox[]" value="option 1" />
<span style="font-size:medium">@option.Text</span>
</label>
<br />
}
</div><span class="clearfix"></span>
</div>
}
else
{
<div class="element-checkbox" single>
<hr>
<h4>@item.Name</h4>
<div class="column column1">
@foreach (var option in item.Options)
{
<label>
<input type="checkbox" name="checkbox[]" value="option 1" />
<span style="font-size:medium">@option.Text</span>
</label>
<br />
}
</div><span class="clearfix"></span>
</div>
}
}
And also it would help if someone tells me how to set the HasMultipleAnswers bool into the checkbox, so that when its true you can mark more than 1 answer, when false you can choose only 1 Thank you :)
Aucun commentaire:
Enregistrer un commentaire