Posting Data to MVC Controller List Of

Kmcnet 706 Reputation points
2023-04-29T02:47:46.92+00:00

Hello everyone and thanks for the help in advance. I'm trying to post data to a controller using a model that is a list. The controller:


        [HttpPost]
        public IActionResult InsertReviewOfSystems(List<tbl_Log_ReviewOfSystems> model)
        {

            return Content(model.Count.ToString());
        }

The model:


    public class tbl_Log_ReviewOfSystems
    {
        public int? ID { get; set; }
        public int? ClientID { get; set; }
        public int? VisitID { get; set; }
        public int? MRNumber { get; set; }
        public int? QuestionID { get; set; }
        public int? CategoryCode { get; set; }
        public string? Category { get; set; }
        public string? EnglishDescription { get; set; }
        public string? QuestionResponse { get; set; }
        public DateTime? DateEntered { get; set; } = DateTime.Now;
}

Sample data posted:

ClientID: 1

VisitID: 9999999

MRNumber: 5396

QuestionID: 8112

CategoryCode: 1

Category: General

EnglishDescription: BMI

QuestionResponse: None

EnteredBy: XXX

ClientID: 1

VisitID: 9999999

MRNumber: 5396

QuestionID: 2092

CategoryCode: 1

Category: General

EnglishDescription: BODYACHES

QuestionResponse: None

EnteredBy: XXX

The return from the controller is 500 error, NullReferenceException: Object reference not set to an instance of an object. Any help would be appreciated.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,419 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,651 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 27,696 Reputation points
    2023-04-29T10:50:02.0566667+00:00

    Your example data does not match the model or explain what is submitting the data. I'll assume a View posting an HTML form. The element names in the form must be indexed so the model binder can populate the list. https://www.learnrazorpages.com/razor-pages/model-binding

        public class SystemReviewController : Controller
        {
            public IActionResult Index()
            {
                List<tbl_Log_ReviewOfSystems> list = PopulateList();
                return View(list);
            }
            [HttpPost]
            public IActionResult InsertReviewOfSystems(List<tbl_Log_ReviewOfSystems> model)
            {
    
                return Ok(model);
            }
    
            private List<tbl_Log_ReviewOfSystems> PopulateList()
            {
                return new List<tbl_Log_ReviewOfSystems>() {
                    new tbl_Log_ReviewOfSystems()
                    {
                        ClientID = 1,
                        VisitID= 9999999,
                        MRNumber= 5396,
                        QuestionID= 8112,
                        CategoryCode= 1,
                        Category= "General",
                        EnglishDescription= "BMI",
                        QuestionResponse= "None",
                        DateEntered= DateTime.Now,
                    },
                    new tbl_Log_ReviewOfSystems()
                    {
                        ClientID = 1,
                        VisitID= 9999999,
                        MRNumber= 5396,
                        QuestionID= 2092,
                        CategoryCode= 1,
                        Category= "General",
                        EnglishDescription= "BODYACHES",
                        QuestionResponse= "None",
                        DateEntered= DateTime.Now,
                    }
                };
            }
        }
    
    @model List<MvcDemo.Models.tbl_Log_ReviewOfSystems>
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h1>Index</h1>
    
    <form method="post" action="/SystemReview/InsertReviewOfSystems">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        @Html.LabelFor(model => Model[0].ClientID)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].VisitID)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].MRNumber)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].QuestionID)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].CategoryCode)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].Category)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].EnglishDescription)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].QuestionResponse)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].DateEntered)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @for (var i = 0; i < Model.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].ClientID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].VisitID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].MRNumber)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].QuestionID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].CategoryCode)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].Category)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].EnglishDescription)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[i].QuestionResponse)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => Model[i].DateEntered)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <div>
            <input type="submit" value="submit" />
        </div>
    </form>
    
    
    

1 additional answer

Sort by: Most helpful
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2023-05-01T05:32:49.07+00:00

    Hi @Kmcnet,

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

    The error is because you haven't defined Model in your View.
    You need to fill your model and send it to the View.

    From the information you have provided so far, it is unclear how you implemented the post data. It would be best if you could provide more detailed information.

     [HttpGet]
            public IActionResult InsertReviewOfSystems()
            {
                
                return View(new List<tbl_Log_ReviewOfSystems>());
            }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments