Click here to Skip to main content
15,936,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to post a list of objects from View to controller.

Below is my code:

@using Models
    @model IList<Add>
        ViewData["Title"] = "AddNewFields";
    <form asp-controller="Trans" asp-action="InsertFields" method="post" class="form-horizontal card-body" role="form">
<td><input type="text" class="form-control" value="Field Size" asp-for="@Model[i].TypeFlag"/></td>
                            <td><input type="text" class="form-control" value="Field Value" asp-for="@Model[i].FieldValue"/></td>
                            <td><input type="text" class="form-control" value="Field Format" asp-for="@Model[i].FieldFormat"/></td>

I will be adding mo these text fields again on button click.


public class Add
        public string TypeFlag { get; set; }

        public string FieldValue { get; set; }

        public string FieldFormat { get; set; }


<pre>public string InsertFields(IList<Add> fields)
            //some logic

When I run the application, I am getting the below error:

NullReferenceException: Object reference not set to an instance of an object.

    AspNetCore.Views_Trans_Add.<ExecuteAsync>b__27_0() in AddNewFields.cshtml

            @for (int i = 0; i < Model.Count; i++)

Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.RunAsync(TagHelperExecutionContext executionContext)

What I have tried:

Please help me...I am stuck here from 1 week..
Updated 14-Aug-18 6:46am

1 solution

You don't seem to have passed a List<add> to the view, so you get the error that you do. Your Razor code doesn't check to see if it actual got an IList<add> object. You just assume that you do and you start trying to get elements from a collection that doesn't even exist so the call to .Count with throw the exception you're seeing.

This is the easiest error to track down using the debugger. You better get to know the debugger very well because it a tool to debug YOU. It's there so you can investigate and understand your code. The reason you have the error in the first place is because you're just guessing at what you're code is doing instead of learning about it and understanding it.

I can't tell you why the Razor code didn't get a collection because you didn't show the code that builds the collection and passes it to the view.

If you don't know how to pass an object to a view, you REALLY need to go back to learning the basics of MVC. This is beginner level stuff here.
return SomeView(objectToPassToModel);
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900