0

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>
}
Yogesh M
  • 43
  • 2
  • 9
  • Your view is creating form controls for a single `ForTestingOnly`, not a collection. You need to initialize a new `List` in the GET method, populate it with 2 items and and return it to the view, then use a `for` loop or `EditorTemplate` to correctly generate you form controls (refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943). –  May 12 '17 at 21:58
  • @StephenMuecke Can you suggest me another example, please. – Yogesh M May 13 '17 at 13:16

1 Answers1

0

Change your Model tags like so

@Html.EditorFor(model => model[counter].Name, new { htmlAttributes = new { @class = "form-control" } })
Travis Acton
  • 4,292
  • 2
  • 18
  • 30
  • model[counter] is underline red. I really need this, Can you help me? – Yogesh M May 13 '17 at 13:15
  • You don't show your @model declaration in your view. Without knowing what you have there it makes it a bit hard but I'm guessing your not declaring it as a list. Take a look at this other thread. It should help. http://stackoverflow.com/questions/17037858/how-to-pass-ienumerable-list-to-controller-in-mvc-including-checkbox-state – Travis Acton May 14 '17 at 03:30
  • Dear Sir, thank you very much for you help, it worked. – Yogesh M May 14 '17 at 10:26
  • Glad to hear, please mark mine or Stephen's answer (he was explaining the same concept) as accepted when you get a chance. – Travis Acton May 14 '17 at 12:28