Если вы преобразуете свой цикл foreach
в своем View в цикл for
, вы можете связать свои значения, используя тот же индекс i
для ваших входных имен:
@for (int i = 0; i < Model.Questions.Count(); i++)
{
@Html.HiddenFor(model => model.Questions[i].QuestionId)
<div class="row m-0 bg-silver-light border">
<div class="col-12 bg-light pt-2 pb-2">
<h6>
@Model.Questions[i].QuestionTitle
</h6>
<p class="text-secondary small m-0">
@Model.Questions[i].QuestionBody
</p>
</div>
<div class="col-12 pt-2 pb-2">
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 20, htmlAttributes: new { @class = "form-check-input", @id = "radio2"})
20
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio3">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 40, htmlAttributes: new { @class = "form-check-input", @id = "radio3" })
40
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio4">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 60, htmlAttributes: new { @class = "form-check-input", @id = "radio4" })
60
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio5">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 80, htmlAttributes: new { @class = "form-check-input", @id = "radio5" })
80
</label>
</div>
<div class="form-check-inline">
<label class="form-check-label" for="radio6">
@Html.RadioButtonFor(model => model.Questions[i].Answer, 100, htmlAttributes: new { @class = "form-check-input", @id = "radio6" })
100
</label>
</div>
</div>
</div>
}
И предполагаяу вашей ViewModel есть свойство Questions
, например:
public class QuestionReviewViewModel
{
public List<QuestionModel> Questions { get; set; } = new List<QuestionModel>();
}
Вы можете привязать значения QuestionId и Answer к своей QuestionModel:
public class QuestionModel
{
public int QuestionId { get; set; }
public int Answer { get; set; }
}
Просто связав ViewModel в вашемHttpPost
action:
[HttpPost]
public ActionResult PostAction(QuestionReviewViewModel vm)
{
if (ModelState.IsValid)
{
for (int i = 0; i < vm.Questions.Count; i++)
{
int id = vm.Questions[i].QuestionId;
int answer = vm.Questions[i].Answer;
}
return Content("Something good");
}
else
{
return Content("Something rotten");
}
}
Опубликовать пример назад
EDIT
Я бы использовал SelectList в этом случае-> https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.selectlist?view=aspnet-mvc-5.2
Вы бы добавили свойство SelectList к своей QuestionModel (аргументы см. В документе выше), которое позволяет вам установить выбранное значение (Answer
в этом случае):
// List of possible answers to this question
// populated from data source
public List<int> AnswerList = new List<int> { 20, 40, 60, 80, 100 };
// An MVC SelectList Class
public SelectList PossibleAnswers => new SelectList(AnswerList, Answer);
Затем, по вашему мнению, вы можете просмотреть возможные ответы, условно применяя свойство selected, если выбран элемент:
<div class="col-12 pt-2 pb-2">
@foreach (var item in Model.Questions[i].PossibleAnswers)
{
var htmlAttr = new Dictionary<string, object>{{ "@class", "form-check-input" }};
if (item.Selected)
{
htmlAttr.Add("@checked", true);
}
<div class="form-check-inline">
<label class="form-check-label" for="radio2">
@Html.RadioButtonFor(model => model.Questions[i].Answer, item.Text, htmlAttributes: htmlAttr)
@item.Text
</label>
</div>
}
</div>