Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC 4 Strongly typed ViewModel containing Strongly typed Model with EditorFor and EditorTemplate partial view not binding

There has been a lot of questions about this... but somehow I can't get this binding to work and I'm still getting null values in my posted View Model. This is MVC 4.

Here is the Main View Model

public class RoleVM {

[Required]
[Display(Name = "Name of the Role")]
public string Role {get; set;}

public IEnumerable<RolePermission> permissions { get; set; }

}

Here is the RolePermission Class

public class RolePermission {

public int id;

public bool permission_value;

public string name { get; set; } 

}

Here is GET Create Method in the controller

public ActionResult Create() {

        RoleVM role_vm = new RoleVM();

        var allpermissions = from p 
        in permission_repo.GetPermissions()
        select p; 

        role_vm.permissions = from p 
        in allpermissions 
        select new RolePermission
        { name = p.name, id = p.PermissionId, permission_value = false };

        return View(role_vm);
    }

Here is the Create.cshtml file

@model RoleVM
@using (Html.BeginForm("Create", "Role", 
FormMethod.Post, new { @class = "permission_form" }))
{
@Html.ValidationSummary(true)

<fieldset>
    <legend>RoleVM</legend>

    <div class="form-item">
        @Html.LabelFor(model => model.Role)

        @Html.EditorFor(model => model.Role)
        @Html.ValidationMessageFor(model => model.Role)
    </div>

   @Html.EditorFor(model => model.permissions)

    <p>
        <input class="submit-btn" type="submit" value="Create" />
    </p>
</fieldset>
}

Next here is the rolepermissions.cshtml file located in ~/Views/Shared/EditorTemplates

@model RolePermission

<div class="form-item">
      @Html.HiddenFor(modelItem => modelItem.id) 
      @Html.LabelFor(modelItem => modelItem.permission_value, Model.name)
      @Html.CheckBoxFor(modelItem => modelItem.permission_value) 
</div>

Here is an example of one of the html items that is rendered on page

<div class="form-item">

      <input data-val="true" data-val-number="The field Int32 must be a number." data-val-required="The Int32 field is required." id="permissions_2__id" name="permissions[2].id" type="hidden" value="3" /> 

      <label for="permissions_2__permission_value">Role-Edit</label>
      <input data-val="true" data-val-required="The Boolean field is required."  id="permissions_2__permission_value" name="permissions[2].permission_value"
 type="checkbox" value="true" /><input name="permissions[2].permission_value"  type="hidden" value="false" /> 

</div>

Finally here is the Create POST method

    [HttpPost]
    public ActionResult Create(RoleVM rolevm)
    {

        //In here rolevm.role is populated based on the textbox input
        //However rolevm.permissions is there with the correct 
        //number of items, but the values all are not binded
        // id is null, name is empty, and permission_value is false 
        // regardless of which checkboxes were checked 


        return RedirectToAction("Index");
    }

Any help on the binding issue with the posted model would be really great.

like image 945
Dane W Avatar asked Jul 14 '12 20:07

Dane W


1 Answers

This was a simple issue, if someone else comes across it maybe this will help them. I didn't have the properties on the class declared as properties with get; set;

public class RolePermission {

public int id { get; set; } 

public bool permission_value { get; set; } 

public string name { get; set; } 

}
like image 179
Dane W Avatar answered Oct 14 '22 21:10

Dane W