Reputation: 62
My View page, it contains many dynamic controls I know this question has been asked and answered a dozen of times but none of the solutions help me.
I have a following ViewModel which is consisted of QuestionBatch data model,listResponseTag and a list of listQuestion data model.
View Model
public class VM_Questionaire
{
public QuestionBatch ThequestionBatch { get; set; }
public List<Question> listQuestion { get; set; }
public List<ResponseTag> listResponseTag { get; set; }
}
Controller
[HttpPost] [ValidateAntiForgeryToken] public ActionResult submit_Questionaire(VM_Questionaire vm_question) { if (ModelState.IsValid) { Console.Write(Newtonsoft.Json.JsonConvert.SerializeObject(vm_question)); } return View("Index"); }
View
<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="[email protected]" id="[email protected]" 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="[email protected]">
<input type="radio" class="form-check-input" id="[email protected]" 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="[email protected]">
<input type="checkbox" class="form-check-input" id="[email protected]" 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>
First object getting value, but the list getting null.
Upvotes: 0
Views: 343
Reputation: 530
if you're not passing any type of data then make your data as a hidden field and after that you will received your data at your respective controller.
Thanks.
Upvotes: 0
Reputation: 43
@Html.DisplayFor() will not post values. You need to use @Html.HiddenFor() along with @Html.DisplayFor(). Refer: Html.DisplayFor not posting values to controller in ASP.NET MVC 3
Upvotes: 0