c# - Error When Posting Back From Drop Down .NET MVC 5 -


i new .net mvc have spent great deal of time crawling through other posts on answers problem haven't been able find anything.

i working on extending identity 2.0 sample project. have been able implement drop-down using html helper on create form new applicationuser, cannot move past error "there no viewdata item of type 'ienumerable' has key 'teamlist'". can see issue - viewmodel , model applicationuser have different type team property (icollection), have no idea it.

my code below:

snippet applicationuser model:

    [display(name = "first name")]     public string firstname { get; set; }     [display(name = "last name")]     public string lastname { get; set; }     [display(name = "hawks number")]     public int number { get; set; }     [display(name = "successful logins")]     public int successfullogins { get; set; }     [display(name = "password status")]     public bool temppassword { get; set; }      public virtual icollection<team> teams { get; set; } 

snippet registerviewmodel viewmodel:

    [display(name = "assigned teams")]     public icollection<team> teams { get; set; } 

snippet useradmincontroller controller:

// get: /users/create     public async task<actionresult> create()     {         //get list of roles         viewbag.roleid = new selectlist(await rolemanager.roles.tolistasync(), "name", "name");         //get list of teams         identitydb _db = new identitydb();         viewbag.teams = new selectlist(_db.teams, "name", "name");          return view();     }      //     // post: /users/create     [httppost]     public async task<actionresult> create(registerviewmodel userviewmodel, params string[] selectedroles)     {         identitydb _db = new identitydb();          // todo: resolve issue team selection          if (modelstate.isvalid)         {             var user = new applicationuser             {                 username = userviewmodel.email,                 email = userviewmodel.email,                 firstname = userviewmodel.firstname,                 lastname = userviewmodel.lastname,                 number = userviewmodel.number,                 successfullogins = 0,                 temppassword = true,             };             var adminresult = await usermanager.createasync(user, userviewmodel.password);              //add user selected roles              if (adminresult.succeeded)             {                 if (selectedroles != null)                 {                     var result = await usermanager.addtorolesasync(user.id, selectedroles);                     if (!result.succeeded)                     {                         modelstate.addmodelerror("", result.errors.first());                         viewbag.roleid = new selectlist(await rolemanager.roles.tolistasync(), "name", "name");                         return view();                     }                 }             }             else             {                 modelstate.addmodelerror("", adminresult.errors.first());                 viewbag.roleid = new selectlist(rolemanager.roles, "name", "name");                 return view();              } 

snippet view:

<div class="form-group">     @html.labelfor(m => m.teams, new { @class = "col-md-2 control-label" })     <div class="col-md-10">         @html.dropdownlistfor(m => m.teams, (ienumerable<selectlistitem>)viewbag.teams, "- please select team -", new { @class = "form-control" })     </div> </div> 

it took while move past "cannot implicitly convert" error point. have tried using linq attempt find team record of course viewmodel never picks http post.

any appreciated. if there more information can provide please let me know.

the error occurs because in post method, return view, have not reassigned value viewbag.teams (as did in method) null. dropdownlist trying bind collection cannot done (a <select> element posts value of selected option) , binding fail.

its unclear code use teams since not referred in post method, need change view model to:

public class registerviewmodel {   [display(name = "assigned teams")]   [required(errormessage = "please select team")]   public string selectedteam { get; set; }   public selectlist teamslist { get; set; }   .... } 

and in method, initialize model , return it

registerviewmodel model = new registerviewmodel(); model.teamslist = new selectlist(_db.teams, "name", "name"); return view(model); 

and in view

@html.labelfor(m => m.teams, new { @class = "col-md-2 control-label" }) @html.dropdownlistfor(m => m.selectedteam, model.teamslist, "- please select team -", new { @class = "form-control" }) @html.validationmessagefor(m => m.teams) 

and in post method, if need return view because modelstate invalid, reassign selectlist before returning view

[httppost] public async task<actionresult> create(registerviewmodel model, params string[] selectedroles) {     ....     // if need return view,     model.teamslist = new selectlist(_db.teams, "name", "name");     return view(model); } 

side note: since using view model, should include properties selecting roles , delete params string[] selectedroles parameter post method.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -