In the view there is a for loop that generates 4 forms with only one submit button, when the button is clicked every entry made in the form are posted.
In my HttpPost Controller, I did the following:
[HttpPost]
public ActionResult Index(List<ForTestingOnly> receivedData)
{
receivedData.ForEach(delegate (ForTestingOnly data)
{
get_Description += data.Description + ", ";
get_Name += data.Name + ", ";
});
ViewData["P_Description"] = get_Description;
ViewData["P_Name"] = get_Name;
return View();
}
I got the following error:
NullReferenceException was unhandled by user code An exception of type 'System.NullReferenceException' occured in MVC_Application.dll but was not handled in user code. Additonal information: Object reference not set to an instance of an object.
I tried another approach:
[HttpPost]
public ActionResult Index(List<ForTestingOnly> receivedData)
{
foreach (ForTestingOnly data in receivedData)
{
get_Description += data.Description + ", ";
get_Name += data.Name + ", ";
}
ViewData["P_Description"] = get_Description;
ViewData["P_Name"] = get_Name;
return View();
}
Again I got the following error:
NullReferenceException was unhandled by user code An exception of type 'System.NullReferenceException' occured in MVC_Application.dll but was not handled in user code. Additonal information: Object reference not set to an instance of an object.
Afterward I added the following in the HttpPost Controller:
receivedData = new List<ForTestingOnly>();
After adding the above code, the error message disappeared. However, when I tried to display the entered data in the textbox in the view after the submit button is clicked, Nothing appear.
below is the Model and View:
Model:
public class ForTestingOnly
{
[Display(Name = "Description: ")]
[Required(ErrorMessage = "The description of the sub event is required.")]
public string Description { get; set; }
[Display(Name = "Name: ")]
[Required(ErrorMessage = "The name is required.")]
public string Name { get; set; }
}
View:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<h4>Count: @ViewData["Count"]</h4>
<br />
<h4>Posted Description: @ViewData["P_Description"]</h4>
<br />
<h4>Posted Name: @ViewData["P_Name"]</h4>
<br />
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@for(int counter = 0; counter<2; counter++) {
<h5>Form: @(counter + 1)</h5>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}