Моя страница просмотра, она содержит много динамических элементов управления
Я знаю, что этот вопрос задавался и отвечал десятки раз, но ни одно из решений не помогло мне.
У меня есть следующая ViewModel, которая состоит из модели данных QuestionBatch, listResponseTag и модели данных listQuestion.
Просмотр модели
public class VM_Questionaire
{
public QuestionBatch ThequestionBatch { get; set; }
public List<Question> listQuestion { get; set; }
public List<ResponseTag> listResponseTag { get; set; }
}
Контроллер
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult submit_Questionaire(VM_Questionaire vm_question)
{
if (ModelState.IsValid)
{
Console.Write(Newtonsoft.Json.JsonConvert.SerializeObject(vm_question));
}
return View("Index");
}
Посмотреть
<pre>
@model Fonz_Survey.Models.VM_Questionaire
@{
ViewBag.Title = "Questionaire";
}
@using (Html.BeginForm("submit_Questionaire", "Questions", FormMethod.Post))
{
@Html.AntiForgeryToken()
<h2>Questionaire</h2>
<div class="card">
<div class="card-header">
<div class="jumbotron-fluid">
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-6">
<h5 class="text-muted text-monospace">CODE</h5>
</div>
<div class="col-lg-3 col-md-6 col-sm-6 text-danger text-monospace">
@Html.DisplayFor(model => model.ThequestionBatch.Code, new { @class = "text-danger text-monospace" })
@Html.HiddenFor(model => model.ThequestionBatch.Code)
</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-6">
<h5 class="text-muted text-monospace">Question Batch</h5>
</div>
<div class="col-lg-3 col-md-6 col-sm-6 text-primary text-monospace">
@Html.DisplayFor(model => model.ThequestionBatch.BatchName, new { @class = "text-primary text-monospace" })
@Html.HiddenFor(model => model.ThequestionBatch.BatchName)
</div>
<div class="col-lg-3 col-md-6 col-sm-6">
<h5 class="text-muted text-monospace">No of Questions</h5>
</div>
<div class="col-lg-3 col-md-6 col-sm-6">
<label class="text-primary text-monospace">@ViewBag.totalQstns</label>
</div>
</div>
<div class="row">
<div class="col-lg-3 col-md-6 col-sm-6">
<h5 class="text-muted text-monospace">Description</h5>
</div>
<div class="col-lg-3 col-md-6 col-sm-6 text-primary text-monospace text-wrap">
@Html.DisplayFor(model => model.ThequestionBatch.Description, new { @class = "text-primary text-monospace text-wrap" })
@Html.HiddenFor(model => model.ThequestionBatch.Description)
</div>
</div>
</div>
</div>
</div>
<div class="card-body">
@{
//int rowIndex = 0;
}
@if (Model != null && Model.listQuestion != null)
{
for(var rowIndex=0; rowIndex< Model.listQuestion.Count; rowIndex++)
//foreach (Question question in Model.listQuestion)
{
// rowIndex++;
<div class="row mb-4">
<div class="col-12">
<div class="card">
<div class="card-header bg-gray text-light">
<div class="d-inline-block">
<label class="text-lg-left font-weight-bold">@rowIndex.</label>
<label class="text-lg-left font-weight-bold">@Model.listQuestion[rowIndex].QuestionEN</label>
<br />
<label class="text-lg-left font-weight-bold">@Model.listQuestion[rowIndex].QuestionAR</label>
</div>
</div>
<div class="card-body font-weight-bolder">
@switch (@Model.listQuestion[rowIndex].QType)
{
case 1:
// to do Text Boxes
<p class="text-info"><u>Please enter your message below;</u></p>
<div class="form-row">
@*<input type="text" placeholder="@question.QuestionEN" name="Q_@question.Id" id="Q_@question.Id" class="form-control" />*@
@Html.EditorFor(model=> @Model.listQuestion[rowIndex].QuestionEN)
</div>
break;
case 2:
// to do Radio
<p class="text-info"><u>Please select any one of the option below;</u></p>
foreach (ChoiceTag ct in Model.listQuestion[rowIndex].ChoiceTags)
{
<div class="form-check">
<label class="form-check-label" for="C_@ct.Id">
<input type="radio" class="form-check-input" id="C_@ct.Id" name="@Model.listQuestion[rowIndex].Id" value="@ct.AnswerEN">@ct.AnswerEN | @ct.AnswerAR
</label>
</div>
}
break;
case 3:
// to do Radio
<p class="text-info"><u>Please select options below;</u></p>
foreach (ChoiceTag ct in Model.listQuestion[rowIndex].ChoiceTags)
{
<div class="form-check">
<label class="form-check-label" for="C_@ct.Id">
<input type="checkbox" class="form-check-input" id="C_@ct.Id" name="@Model.listQuestion[rowIndex].Id" value="@ct.AnswerEN">@ct.AnswerEN | @ct.AnswerAR
</label>
</div>
}
break;
}
</div>
</div>
</div>
</div>
}
<div class="form-group">
<div class="col-12 text-center">
<input type="submit" value="Submit" class="btn btn-success " />
</div>
</div>
}
</div>
<div class="card-footer">
<label class="text-danger">@ViewBag.Message</label>
</div>
</div>
}
</prev>
Первый объект получает значение, но список становится нулевым.