asp.net mvc - How to compare viewbag value with model another value in mvc 4 -
i trying send viewbag action method view. when first page loads viewbag value null. when call checkpermissions action method viewbag gets value , return same view time viewbag contains value , want compare viewbag value value. tried following error appearing. cannot perform runtime binding on null reference. index view code.
@model c3card.models.grouppermissionvm @{ viewbag.title = "index"; } @using (html.beginform()) { @html.labelfor(m=>m.groupid) @html.dropdownlistfor(m => m.groupid, model.grouplist, "please select", new { id = "ddlgrp" }) foreach(var permission in model.permissions) { if (viewbag.marlid.equals(permission)) { <label> @html.radiobuttonfor(m => m.perm_id, permission.perm_id, new {@checked="true"}) <span>@permission.perm_levelname</span> </label> } else { <label> @html.radiobuttonfor(m => m.perm_id, permission.perm_id) <span>@permission.perm_levelname</span> </label> } }
this action method
public actionresult checkpermissions(int id) { var groups = db.tm_grp_group.where(a => a.grp_isactive == true); var permissions = db.tm_perm_level; grouppermissionvm model = new grouppermissionvm { marlid=db.ts_grp_perm_mapping.select(p=>p.grp_id==id).count(), grouplist = new selectlist(groups, "grp_id", "grp_name"), permissions = permissions.select(p => new permissionvm { perm_id = p.perm_id, perm_levelname = p.perm_levelname }) }; viewbag.marlid = db.ts_grp_perm_mapping.select(p => p.grp_id == id).count(); return view("index",model); }
any suggestion why not able compare values inside if condition? in advance. line causing me error if (viewbag.marlid.equals(permission)) edited follows
foreach(var permission in model.permissions) { if(model.marlid==permission.perm_id) { <label> @html.radiobuttonfor(m => m.perm_id, permission.perm_id,new { @checked = true } )@model.marlid.tostring() <span>@permission.perm_levelname</span> </label> }
you can change code given below
if(viewbag.marlid != null && model.permissions != null) { foreach(var permission in model.permissions) { if(viewbag.marlid == permission.perm_id) { <label> @html.radiobuttonfor(m => m.perm_id, permission.perm_id,new { @checked = true } )@model.marlid.tostring() <span>@permission.perm_levelname</span> </label> } } }
hope helps.
Comments
Post a Comment