Posting Data to MVCC ViewModel

Kmcnet 806 Reputation points
2023-05-02T02:17:48.1033333+00:00

This is an offshoot from a previous question I posted. Assuming I want to post a collection to a List Of that looks like this:

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;
}

But I want to post an additional input field that is a string called EnteredBy via an <input> field, I'm thinking I would create a ViewModel:

    public class ReviewOfSystemsVM
    {
        public string? EnteredBy { get; set; }
        public List<tbl_Log_ReviewOfSystems>? tbl_Log_ReviewOfSystems { get; set; }
    }

and then post to it, however, I am now unable to reference the List in the controller, receiving an object not set to reference error. The list works fine when using it by itself as the model. Any help would be appreciated.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,674 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,540 questions
{count} votes

Accepted answer
  1. Xinran Shen - MSFT 2,091 Reputation points
    2023-05-02T04:08:26.03+00:00

    Hi @Kmcnet, I change the code according to your previous question to fit your new requirement, Hoping it can give you some help.

    Model

    public class ReviewOfSystemsVM
        {
            public string? EnteredBy { get; set; }
            public List<tbl_Log_ReviewOfSystems>? tbl_Log_ReviewOfSystems { get; set; } = new List<tbl_Log_ReviewOfSystems>() { };
        }
    

    Controller

    public class SystemReviewController : Controller
        {
            public IActionResult Index()
            {
                ReviewOfSystemsVM reviewOfSystemsVM = new ReviewOfSystemsVM();
                reviewOfSystemsVM.tbl_Log_ReviewOfSystems = PopulateList();
                return View(reviewOfSystemsVM);
            }
            [HttpPost]
            public IActionResult InsertReviewOfSystems(ReviewOfSystemsVM 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,
                    }
                };
            }
        }
    

    View

    @model ReviewOfSystemsVM
    
    <h1>Index</h1>
    
    <form method="post" action="/SystemReview/InsertReviewOfSystems">
    
        <div>
            @Html.LabelFor(Model=>Model.EnteredBy)
            @Html.TextBoxFor(Model=>Model.EnteredBy)
        </div>
        <table class="table">
            <thead>
                <tr>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].ClientID)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].VisitID)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].MRNumber)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].QuestionID)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].CategoryCode)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].Category)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].EnglishDescription)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].QuestionResponse)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model.tbl_Log_ReviewOfSystems[0].DateEntered)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @for (var i = 0; i < Model.tbl_Log_ReviewOfSystems.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].ClientID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].VisitID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].MRNumber)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].QuestionID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].CategoryCode)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].Category)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].EnglishDescription)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].QuestionResponse)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem =>  Model.tbl_Log_ReviewOfSystems[i].DateEntered)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <div>
            <input type="submit" value="submit" />
        </div>
    </form>
    

    enter image description here


    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.

    Best regards,

    Xinran Shen


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.