Reputation: 195
I'm having an issue trying to pass a model from partial view to controller for further processing.
This is my controller:
public ActionResult Index()
{
IList<ActionRoleEntity> permissionCollection = new SecurityManager().GetPermissionCollection();
var model = new PermissionsModel
{
PermissionCollection = permissionCollection
};
return View(model);
}
This one in my Index view. which as you would see includes a partial view _Permissions.
@model WebApp.ViewModels.PermissionsModel
<script>
$(document).ready(function () {
$('#AdminTabs').tabs();
});
</script>
<div id="AdminTabs">
<ul>
<li><a href="#tabs-1">Permissions</a></li>
<li><a href="#tabs-2">Insights</a></li>
</ul>
<div id="tabs-1">
@{ Html.RenderPartial("_Permissions"); }
</div>
<div id="tabs-2">
<p>
</p>
</div>
</div>
And this is my partial view:
@model WebApp.ViewModels.PermissionsModel
@using (Html.BeginForm("Permissions", "Admin", FormMethod.Post))
{
<table id="example" class="display">
<thead>
<tr>
<th>Role</th>
<th>Workflow</th>
<th>Stage</th>
<th>Action</th>
<th>Read</th>
<th>Update</th>
<th></th>
</tr>
</thead>
<tbody>
@for (var i = 0; i < Model.PermissionCollection.Count; i++)
{
<tr>
<td>@Html.DisplayTextFor(m => m.PermissionCollection[i].RoleName)</td>
<td>@Html.DisplayTextFor(m => m.PermissionCollection[i].Workflow)</td>
<td>@Html.DisplayTextFor(m => m.PermissionCollection[i].StageName)</td>
<td>@Html.DisplayTextFor(m => m.PermissionCollection[i].ActionName)</td>
<td>@Html.CheckBoxFor(m => m.PermissionCollection[i].Get)</td>
<td>@Html.CheckBoxFor(m => m.PermissionCollection[i].Post)</td>
<td>@Html.HiddenFor(m => m.PermissionCollection[i].hasChanged)</td>
</tr>
}
</tbody>
</table>
<input type="submit" id="btnSubmit" name="btnSubmit" value="Save" />
}
The model is properly populated for the controller and the html code in the partial view is rendered without any problems. However when I submit the the form by clicking the submit button in _permissions Partial View ... the method "Permissions" in the controller "Admin" that handles that action receives an empty "PermissionModel" model.
This is the code for that action:
[HttpPost]
public ActionResult Permissions(PermissionsModel permissionsModel)
{
IList<ActionRoleEntity> actionRoleEntity = permissionsModel.PermissionCollection.Where(m => m.hasChanged == true).ToList<vw_ActionRoleEntity>();
// Save changed permission to database
return View("Index");
}
And finally this is how the Model for Permissions has been coded:
public class PermissionsModel
{
public IList<ActionRoleEntity> PermissionCollection { get; set; }
}
Javascript to update the hasChanged property of an ActionRoelEntity.
$("#example input[type='checkbox'").change(function () {
var arrayCheckboxId = $(this).attr('id').split('_');
var index = arrayCheckboxId[2]; // Gets specific row Id.
$('#PermissionCollection_' + index + '__hasChanged').val(true);
});
If you see something I may be missing please advice.
Upvotes: 1
Views: 3305
Reputation: 36
Have a look on how your model is name in the Post method and in the view. I see that you called model on the view @model WebApp.ViewModels.PermissionsModel and in your lambda assignments the model is m, and on your post method is permissionsModel. change it to model in both places.
Upvotes: 2