Search code examples
c#.netpostmodel-view-controllerhtml.beginform

I can't see the input value in the input in html controller


The model of my html page is in the form of a list. I want to enter the exam grade here, but when I entered the exam grade, my data does not go to my actionResult type function. In fact, when I write an integer value to the input, it does not save its value. The database also lists the values I have written manually, but still does not bring the data to the function.

my html code:

@model IEnumerable<OnlineBasvuru.Entity.ViewModel.ExamViewModal>

@{
    Layout = null;
}
<div>
    @using (Html.BeginForm("Save", "Exam", FormMethod.Post))
    {
        <table class="table">
            <thead>
                <tr> 
                    <th>
                        StudentId
                    </th>
                    <th>
                        Note
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.ToList())
                {
                    <tr>
                        <td>
                            @Html.Label(item.StudentId.Value.ToString())
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => item.Note)
                        </td>
                    </tr>
                }

            </tbody>

        </table>
        <button type="submit">Save</button>
    }
</div>

my controller code:

  [HttpPost]
        public ActionResult Save(ICollection<ExamViewModal> nwModal) // Normally nwModal should be listed as a list of the model and the data should be in
            {

            foreach (ExamViewModal modal in nwModal)
            {
                EXAM exam = examService.Get(modal.Id);
                exam.NOTE = modal.Note;
                examService.Save(exam );
            }

            return RedirectToAction("ExamList", "Exam");
        }

Please show me a way I do not know how to do. Thank you.


Solution

  • Try using a for loop instead of a foreach. This will then set the name property using indexes rather than spitting out the same name for each item in the loop.

    For example:

    for (int i = 0; i < Model.Count(); i++) {
       Html.EditorFor(m => Model[i].Note)
    }