Databinding Problems

Kmcnet 1,256 Reputation points
2023-06-11T02:06:06.93+00:00

Hello everyone and thanks for the help in advance. I am developing a MVC application that posts a list of a model to a controller. The model:

    public class tbl_Log_Exams     {         public int? ID { get; set; }          public int? ClientID { get; set; }          public int? VisitID { get; set; }          public string? MRNumber { get; set; }          public DateTime? TimeEntered { get; set; }          public string? EnteredBy { get; set; }          public string? Normal { get; set; }          public string? Exceptions { get; set; }          public string? Category { get; set; }          public int? CategoryCode { get; set; }      }

The posted data:

tbl_Log_Exams[0].CategoryCode : 1

tbl_Log_Exams[0].MRNumber : 12345

tbl_Log_Exams[0].VisitID : 98765

tbl_Log_Exams[0].Normal : All

tbl_Log_Exams[0].Exceptions : None

and the receiving controller:

public ActionResult ExamResults(List<tbl_Log_Exams> model

However, on the receiving controller, Model.Count returns zero. I am not sure what the problem is. Any help would be appreciated.

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    2023-06-12T03:27:05.79+00:00

    Hi @Kmcnet,

    You did not provide the front-end code, so it is not clear how you post data,

    you can refer to the following example.

    @model List<MvcDemo612.Models.tbl_Log_Exams>
    
    @{
        ViewBag.Title = "ExamResults";
    }
    
    <h2>ExamResults</h2>
    
    
    <form method="post" action="/Home/ExamResults">
        <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].TimeEntered)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].CategoryCode)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].Category)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].EnteredBy)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].Normal)
                    </th>
                    <th>
                        @Html.LabelFor(model => Model[0].Exceptions)
                    </th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @for (var i = 0; i < Model.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].ClientID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].VisitID)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].MRNumber)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].TimeEntered)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].CategoryCode)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].Category)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].EnteredBy)
                        </td>
                        <td>
                            @Html.TextBoxFor(modelItem => Model[0].Normal)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => Model[0].Exceptions)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
        <div>
            <input type="submit" value="submit" />
        </div>
    </form>
    
     public ActionResult ExamResults()
            {
                List<tbl_Log_Exams> list = List();
                return View(list);
              
            }
            [HttpPost]
            public ActionResult ExamResults(List<tbl_Log_Exams> model)
            {
               
                return Json(model);
            }
            private List<tbl_Log_Exams> List()
            {
                return new List<tbl_Log_Exams>() {
                    new tbl_Log_Exams()
                    {
                        ClientID = 1,
                        VisitID= 1,
                        MRNumber= "123",
                        TimeEntered=DateTime.Now,
                        CategoryCode= 1,
                        Category= "1",
                        EnteredBy= "1",
                        Normal= "All",
                        Exceptions= "None",
                    },
                    new tbl_Log_Exams()
                    {
                        ClientID = 2,
                        VisitID= 2,
                        MRNumber= "123",
                        TimeEntered=DateTime.Now,
                        CategoryCode= 1,
                        Category= "2",
                        EnteredBy= "2",
                        Normal= "All",
                        Exceptions= "",
                    }
                };
            }
    

    User's image

    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.


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,491 Reputation points
    2023-06-11T10:35:11.1533333+00:00

    The action is designed to look for an collection of tbl_Log_Exams.

    [0].ID
    

    Your view design is based on a class with a List<tbl_Log_Exams> tbl_Log_Exams property.

    tbl_Log_Exams[0].ID
    

    The action parameter should be the class type and name that has the tbl_Log_Exams property not "List<tbl_Log_Exams> model".

    Your model is like the following.

        public class MyViewModel
        {
            public List<tbl_Log_Exam> tbl_Log_Exams { get; set; }
        }
        public class tbl_Log_Exam
        {
            public int? ID { get; set; }
        }
    

    Therefore the action parameter should be...

            [HttpPost]
            public ActionResult Index(MyViewModel model)
            {
                return Json(model);
            }
    
    0 comments No comments

Your answer

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