Rajeev.Ranjan
Rajeev.Ranjan

Reputation: 283

Saving multiple records on submit click into differnt entities in MVC4. Not getting values from view in Controller

I am trying to save the class attendance for multiple students on click of submit button. I am able to create the blank records in the concern tables and then populate the data in view.

I have the following view model:

public class TeacherAttendanceModel
{
    #region Required Properties

        public long ScholarAttendanceId { get; set; }
        public string Student { get; set; }
        public bool Absent { get; set; }
        public string AbsentComment { get; set; }
        public bool Uniform { get; set; }
        public bool Homework { get; set; }
        public string HomeworkComment { get; set; }
        public String UniformCommentSelected { get; set; }
        public IEnumerable<String> UniformComment { get; set; }

    #endregion

}

My Controller is as below.

public class TeacherAttendanceController : Controller
{
    //
    // GET: /TeacherAttendance/

    public ActionResult Index()
    {
        long classId = Success.Business.Roles.Teacher.GetHomeRoomClassID(Convert.ToInt64(Session[GlobalVar.LOGGED_IN_ID]));
        var classAttendanceStatus = Success.Business.Entities.ClassAttendance.GetClassAttendanceStatus(classId);
        ViewBag.status = classAttendanceStatus;
        var attendanceData = TeacherAttendance.CreateClassAttendance(classId);
        return View(attendanceData);
    }

    [HttpPost]
    public ActionResult Index(IEnumerable<TeacherAttendanceModel> teacherAttendanceModel)
    {
        try
        {
            if (ModelState.IsValid)
            {
                TeacherAttendance.SaveAttendance(teacherAttendanceModel);
            }
        }
        catch (Exception e)
        {

        }

        return View(teacherAttendanceModel);
    }

}

Get Index is working fine. But I am not getting the TeacheAttendanceModel object in Post index. I get null object. I would be thank full to get any help in this regards. How to update the multiple records of attendance on submit click?

I am using the following View:

@foreach (var item in Model) {
    <tr >
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.DisplayFor(modelItem => item.Student)
        </td>
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.CheckBoxFor(modelItem => item.Absent, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
            @Html.TextBoxFor(modelItem => item.AbsentComment, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
        </td>
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.CheckBoxFor(modelItem => item.Uniform, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
            @Html.DropDownListFor(modelItem => item.UniformCommentSelected, new SelectList(item.UniformComment),item.UniformCommentSelected ?? "---Select---", ViewBag.status == 2? new {disabled = "disabled"} : null)
        </td>
        <td style="border-style:solid; border-color:darkslategray; border-width:thin;">
            @Html.CheckBoxFor(modelItem => item.Homework, ViewBag.status == 2 ? new {disabled = "disabled"} : null)
            @Html.TextBoxFor(modelItem => item.HomeworkComment, ViewBag.status == 2? new {disabled = "disabled"} : null)
        </td>

    </tr>
}

Upvotes: 0

Views: 6816

Answers (2)

Billy Samuel
Billy Samuel

Reputation: 672

Use a IList instead of IEnumerable in the view and replace the foreach loop with a for loop.

Step 1:

Use

@model IList<TeacherAttendanceModel>

instead of

@model IEnumerable<TeacherAttendanceModel>

Step 2:

Use

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

instead of

@foreach (var item in Model)

Refer How to pass IEnumerable list to controller in MVC including checkbox state? for more details.

Upvotes: 0

webdeveloper
webdeveloper

Reputation: 17288

Model:

public class Test
{
    public List<string> UniformComment { get; set; }
}

Controller:

    public ActionResult Index()
    {
        var model = new Test
            {
                UniformComment = new List<string>{ "one", "two", "three" }
            };

        return View(model);
    }

    [HttpPost]
    public ActionResult Index(Test model)
    {
        return View(model);
    }

View:

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.UniformComment.Count; i++)
    {
        @Html.TextBoxFor(x => Model.UniformComment[i])
    }

    <input type="submit" value="Save" />
}

Rendered html example:

<input id="UniformComment_0_" name="UniformComment[0]" type="text" value="one" />
<input id="UniformComment_1_" name="UniformComment[1]" type="text" value="two" />
<input id="UniformComment_2_" name="UniformComment[2]" type="text" value="three" />

The idea is iterate with for loop or create EditorTemplate and then you receive indexed items.

Added (Feel the difference):

View:

@using (Html.BeginForm())
{
    foreach (var comment in Model.UniformComment)
    {
        @Html.TextBoxFor(x => comment)
    }

    <input type="submit" value="Save" />
}

Rendered html:

<input id="comment" name="comment" type="text" value="one" />
<input id="comment" name="comment" type="text" value="two" />
<input id="comment" name="comment" type="text" value="three" />

Upvotes: 1

Related Questions