asp.net mvc - Persist checkbox values on postback in MVC3 -


model:

  public class userviewmodel     {                public userdetail user { get; set; }         public list<int> selectedroles { get; set; }          } 

httpget action:

   public actionresult index()    {         viewbag.roles = list of roles (list<roles>)         return view();    } 

view:

@using (html.beginform("index", "home", formmethod.post)) {            foreach (var item in viewbag.roles)     {        <input id="selectedroles" name="selectedroles" type="checkbox" value="@item.id" >                @item.name     }       <input type="submit" value="submit"/>   } 

httppost action:

 [httppost]   public actionresult createuser(userviewmodel model, formcollection form)   {      foreach (int roleid in model.selectedroles)      {           here getting ids of selectedroles roleid      }   } 

on httppost want persist checkbox values have selected. how in mvc3?

it looks checkboxes bound property on viewbag. have re-assign roles on viewbag in post action, ui can rebound when view rendered after post action has executed. viewbag not persist between different view renderings, must reset


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -