c# - mvc5 model which is posted with html.beginform is missing some properties -
i have model i'm using in view:
using (var db = new fieldsavercontext()) { aspnetuser user = (from p in db.aspnetusers p.id == id select p).include(m => m.aspnetroles) .first(); model.auser = user; list<aspnetrole> roles = (from r in db.aspnetroles select r).tolist(); model.rolelist = roles; list<listid> ids = (from l in db.listids select l).tolist(); model.listidlist = ids; list<viewerlist> viewerlists = (from v in db.viewerlist select v).tolist(); model.viewerlist = viewerlists; }
this how view looks like:
@using (html.beginform("edit", "users")) { <div class="container containeredit"> <div class="col-md-9"> <div class="col-md-6"> <div class="form-group"> @html.labelfor(m => m.auser.id) <label>@html.displayfor(m => m.auser.id)</label> </div> <div class="form-group"> @html.displayfor(m => m.auser.firstname) @html.textboxfor(m => m.auser.firstname) </div> <div class="form-group"> @html.labelfor(m => m.auser.phonenumber) @html.textboxfor(m => m.auser.phonenumber) </div> <div class="form-group"> @html.labelfor(m => m.listidlist) @html.dropdownlistfor(n => n.listidlist, new selectlist(model.listidlist, "id", "value")) </div> <div class="form-group"> @html.labelfor(m => m.auser.note1) @html.textboxfor(m => m.auser.note1) </div> <div class="form-group"> @html.labelfor(m => m.auser.note2) @html.textboxfor(m => m.auser.note2) </div> </div> <div class="col-md-6"> <div class="form-group"> </div> <div class="form-group"> @html.labelfor(m => m.auser.lastname) @html.textboxfor(m => m.auser.lastname) </div> <div class="form-group"> @html.labelfor(m => m.auser.email) @html.textboxfor(m => m.auser.email) </div> <div class="form-group"> <label>report viewer</label> @html.dropdownlistfor(v => v.auser.viewerid, new selectlist(model.viewerlist, "id", "value")) </div> </div> @html.hiddenfor(v => v.auser.id) </div> <div class="col-md-3"> @foreach (aspnetrole role in model.rolelist) { @html.checkbox(role.name, model.auser.aspnetroles.any(userrole => userrole.id == role.id)) @html.label(role.name)<br /> } </div> </div> <div class="col-md-8"> <input type="submit" class="btn btn-success btn-lg btnsaveedit" value="save" /> </div> }
when postback happening, i'm able values auser i'm not getting of values model.rolelist used in checkbox. want check checkboxes checked rolelist , assign roles user.
any idea missing?
thanks in advance, laziale
update:
edit action(s):
[httpget] public actionresult edit(int? id) { manageviewmodel model = new manageviewmodel(); using (var db = new dbcontext()) { aspnetuser user = (from p in db.aspnetusers p.id == id select p).include(m => m.aspnetroles) .first(); model.auser = user; list<aspnetrole> roles = (from r in db.aspnetroles select r).tolist(); model.rolelist = roles; list<listid> ids = (from l in db.listids select l).tolist(); model.listidlist = ids; list<viewerlist> viewerlists = (from v in db.viewerlist select v).tolist(); model.viewerlist = viewerlists; } return view(model); } [httppost] public actionresult edit(manageviewmodel model) { aspnetuser user = model.auser; using (var db = new dbcontext()) { aspnetuser dbuser = (from u in db.aspnetusers u.id == model.auser.id select u).first(); dbuser.firstname = model.auser.firstname; dbuser.lastname = model.auser.lastname; dbuser.email = model.auser.email; dbuser.phonenumber = model.auser.phonenumber; dbuser.note1 = model.auser.note1; dbuser.note2 = model.auser.note2; dbuser.viewerid = model.auser.viewerid; db.savechanges(); } return view(); }
the issue roles not bound view model. here's have do:
create new role view model:
public class roleviewmodel { public int id { get; set; } public string name { get; set; } public bool selected { get; set; } }
modify
manageuserviewmodel
have list ofroleviewmodel
instead ofaspnetrole
:public class manageuserviewmodel { public list<roleviewmodel> roles { get; set;} // other properties }
change edit set
roles
property available roles:public actionresult edit(int? id) { // beginning of action list<aspnetrole> roles = r in db.aspnetroles select r; model.roles = new list<roleviewmodel>(); foreach(aspnetrole role in roles){ model.roles.add(new roleviewmodel { id = role.id, name = role.name, selected = user.aspnetroles.any(userrole => userrole.id == role.id) }); } // end of action }
change other action to
public actionresult edit(manageviewmodel model){ // beginning of action // remove roles user list<aspnetroles> newroles = new list<aspnetroles>(); foreach(roleviewmodel role in model.roles){ newrole.add(db.aspnetroles.find(role.id)) } // set user roles newroles // end of action }
in view change checkbox part :
@html.editorfor(model => model.roles)
and create
shared/editortemplate/roleviewmodel.cshtml
file :@model roleviewmodel @html.hiddenfor(model => model.id) @html.checkboxfor(model => model.selected) @html.displayfor(model => model.name)
Comments
Post a Comment