1

I have a partial view like this:

 @model InventoryControl.Models.InventoryControlModel

 <table class="table_body" style="font-size:x-small">
 @if (Model != null)
{
    for (var rolcnt = 0; rolcnt < Model.Roles.Count(); rolcnt ++)
    {
        <tr>
            <td>
                @Html.DisplayTextFor(x => x.Roles[rolcnt].ItemTypeCode)
            </td>
            <td>
                @Html.CheckBoxFor(x=>x.Roles[rolcnt].Included)
            </td>
        </tr>   
    } 

}
else
{
    <span><i>User has no records for Inventory Control</i></span>   
}

The partial view is populating correctly with the particular item type code and the included flag. I then try to post changes back with this:

 function AddInventoryRole() {
    $.ajax({
        type: 'POST',
        data: $("#InventoryRoleForm").serializeArray(),
        url: '@Url.Action("PostInventoryRoles")',
        success: function () {
            alert('success');
        },
        error: function () {
            alert('error');
        }
    });
}

I am seeing some of the data in the controller method. The item type code is not there, it's NULL for every entry, even the though the included flag is correct in the post. What am I doing wrong?

 [HttpPost]
    public ActionResult PostInventoryRoles(InventoryControlModel model)
    {
        return Json(model);
    }

1 Answer 1

2

The DisplayTextFor is for display only and will not be posted, assuming your current input checkbox works for the Included field.

Simply add a hidden for and it will be posted in the same manner:

 @Html.HiddenFor(x => x.Roles[rolcnt].ItemTypeCode)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.