0

I have to bind checkbox value and textbox value with model.Here I am using model to retrive and to post data as well. I have tried few options but it didnt work.

Below is my code:

@model IEnumerable<ALDS.Web.Areas.AR.Models.ReportViewModel>

@{
    ViewBag.Title = "Standings";
}


    <table class="table table-bordered">
        <thead>
            <tr>
                <th>LR Date</th>
                <th>LR Line No</th>
                <th>Issue</th>
                <th>Correction Response</th>
                <th>Remarks</th>
                <th>Submission</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @item.InsertDate
                    </td>
                    <td>
                        @item.LRLineID <a href="">View</a>
                    </td>
                    <td>Margin % incorrect</td>
                    <td><label for="cbox1">Corrected</label>  <input type="checkbox" name="Corrected" value="@item.Status"  />     <label for="cbox1">Neglected</label>  <input type="checkbox" name="Neglected" value="@item.Status"/></td>
                    <td><input type="text" value="@item.Comments"/></td>
                    <td><a href="@Url.Action("Update","Error",Model)">Submit</a></td>

                    </tr>
            }
        </tbody>
    </table>

I have to send the checkbox and textbox value to controller. Please help.

user3206357
  • 393
  • 2
  • 6
  • 19

1 Answers1

2

Render the inputs using the HtmlHelpers provided by MVC. They will make sure that the id and name attributes of the generated <input/> are in a format that can be processed by the MVC ModelBinder.

Also, to post back lists of objects, use a for loop, so that the items get an index understood by the ModelBinder.

Wrap the inputs in a <form> to be posted to the Controller. With the following example, the model will be posted to the "Update" action in "ErrorController" when the user clicks the "Submit" button. myFormClass and myFormId are not neccessary, I just wanted to show how you could add them if needed.

@using(Html.BeginForm("Update", "Error", FormMethod.Post, new { @class = "myFormClass", id = myFormId })) {
    for (var i = 0; i < Model.Length; i++) {
        @Html.LabelFor(m => m[i].Status)
        @Html.CheckBoxFor(m => m[i].Status)

        @Html.LabelFor(m => m[i].Comments)
        @Html.TextAreaFor(m => m[i].Comments) // multi line 
        @Html.TextBoxFor(m => m[i].Comments)  // single line 
    }

    <button type="submit">Submit</button>
}

LabelFor will try to find [Display(Name="some_resource_key", ResourceType = typeof(Resources))] attributes on the property in the ViewModel to look up the translated text to be used as label in the Resources.resx.

EDIT As Antoine mentioned, you have to provide inputs for ALL ViewModel properties that shall be posted back. You can render <input type="hidden"/> using @Html.HiddenFor(m => m[i].Id).

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36