c# - How to get user from Asp.Net Identity 2? -


i'm new mvc , new identity 2.0. i'm trying create way edit user , i'm not having luck. use poco class created called member can shape way data passed view. however, can't display using applicationuser identity. had working applicationuser @ 1 point when tried changing member didn't work , can't revert applicationuser.

i've searched on sample of doing crud on identity 2.0 haven't found made kind of sense. so, i'm using sample code microsoft's contoso university project , trying tweak work. can list of users in grid.mvc no problem.

any appreciated.

this error i'm getting in view:

the model item passed dictionary of type 'system.data.entity.infrastructure.dbquery`1[testidentity.models.applicationuser]', dictionary requires model item of type 'testidentity.models.applicationuser'.

[httpget]     public actionresult edit(string userid)     {         using (var db = new applicationdbcontext())         {             if (userid == null)             {                 return new httpstatuscoderesult(httpstatuscode.badrequest);             }              //var member = m in db.users             //                 m.id == userid             //                 select new member             //                 {             //                     firstname = m.firstname,             //                     lastname = m.lastname,             //                     email = m.email,             //                     userid = m.id             //                 };               var member = m in db.users                          m.id == userid                          select m;              if (member == null)             {                 return httpnotfound();             }              return view(member);                     }                     } 

this view

    @model testidentity.models.applicationuser  @{     viewbag.title = "edit";     layout = "~/views/shared/_layout.cshtml"; }  <h2>edit</h2>  @using (html.beginform()) {     @html.antiforgerytoken()      <div class="form-horizontal">         <h4>member</h4>         <hr />         @html.validationsummary(true, "", new { @class = "text-danger" })         @html.hiddenfor(model => model.id)          <div class="form-group">             @html.labelfor(model => model.lastname, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.lastname, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.lastname, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.firstname, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.firstname, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.firstname, "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             @html.labelfor(model => model.email, htmlattributes: new { @class = "control-label col-md-2" })             <div class="col-md-10">                 @html.editorfor(model => model.email, new { htmlattributes = new { @class = "form-control" } })                 @html.validationmessagefor(model => model.email , "", new { @class = "text-danger" })             </div>         </div>          <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="save" class="btn btn-default" />             </div>         </div>     </div> }  <div>     @html.actionlink("back list", "index") </div> 

here member.cs class use:

namespace testidentity.models {     public class member     {         public string userid { get; set; }         public string memberid { get; set; }         public string firstname { get; set; }         public string lastname { get; set; }         public string username { get; set; }         public string email { get; set; }         public string phonenumber { get; set; }     } } 

your member iqueryable<applicationuser>, not applicationuser object

var member = m in db.users              m.id == userid              select m; 

there 2 ways fix it:

var members = m in db.users              m.id == userid              select m; var member = members.firstordefault(); // nullable if userid not found. 

or using lambda expression

var member = db.users.firstordefault(u => u.id == userid); // nullable if userid not found. 

Comments