2015-09-26 59 views
0

我對MVC和全新的身份2.0都很新。我正在嘗試創建一種編輯用戶的方式,我沒有太多運氣。最終,我想使用我創建的一個名爲Member的POCO類,以便可以調整數據傳遞給View的方式。但是,現在我甚至無法使用Identity中的ApplicationUser來顯示它。我曾與ApplicationUser一起工作,但後來當我嘗試將其更改爲成員時,它不起作用,現在我無法恢復到ApplicationUser。如何從Asp.Net Identity 2獲取用戶?

我已經搜索了所有在Identity 2.0上做CRUD的樣本,但沒有找到任何有意義的東西。所以,現在我正在使用來自微軟Contoso大學項目的示例代碼,並試圖調整它的工作。我可以在沒有任何問題的情況下獲取Grid.Mvc中的用戶列表。

任何幫助將不勝感激。

這是我收到的查看錯誤:

傳遞到字典的模型項的類型爲「System.Data.Entity.Infrastructure.DbQuery`1 [TestIdentity.Models.ApplicationUser]」 ,但是這個字典需要一個'TestIdentity.Models.ApplicationUser'類型的模型項。

[HttpGet] 
    public ActionResult Edit(string UserID) 
    { 
     using (var db = new ApplicationDbContext()) 
     { 
      if (UserID == null) 
      { 
       return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 
      } 

      //var member = from m in db.Users 
      //     where m.Id == UserID 
      //     select new Member 
      //     { 
      //      FirstName = m.FirstName, 
      //      LastName = m.LastName, 
      //      Email = m.Email, 
      //      UserID = m.Id 
      //     }; 


      var member = from m in db.Users 
         where m.Id == UserID 
         select m; 

      if (member == null) 
      { 
       return HttpNotFound(); 
      } 

      return View(member);    
     }    

    } 

這是查看

@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 to List", "Index") 
</div> 

這裏是我的Member.cs類我想用:

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; } 
    } 
} 
+1

您尚未實現對象。你所稱的成員實際上是一個返回成員對象的查詢。要獲取成員,請在查詢上調用.FirstOrDefault():var actualMember = member.FirstOrDefault()。 – dbugger

回答

1

你的成員是IQueryable<ApplicationUser>,而不是ApplicationUser對象

var member = from m in db.Users 
      where m.Id == UserID 
      select m; 

有兩種方法來解決這個問題:

var members = from m in db.Users 
      where m.Id == UserID 
      select m; 
var member = members.FirstOrDefault(); // nullable if UserId not found. 

或使用lambda表達式

var member = db.Users.FirstOrDefault(u => u.Id == UserId); // nullable if UserId not found. 
+0

謝謝。 lambda表達式起作用。然而,出於學習的目的,我試圖把它轉換成我的Member.cs,所以我可以對數據進行一些修改。 – Caverman

+0

我已經試過像'VAR成員=從米db.Users有兩件事情 其中m.Id ==用戶名 選擇米;'本'VAR成員=從米會員 選擇新會員 { 姓= m.FirstName, LastName = m.LastName, Email = m.Email, UserID = m。Id }; ' – Caverman

+0

也嘗試了類似的東西,但在成員中使用'Member member = from m選擇新成員{FirstName = m.FirstName,LastName = m.LastName,Email = m.Email,UserID = m.Id};'沒有運氣。 – Caverman

0

完美!感謝kienct89。

這是我最終用來獲取用戶,然後將它們映射到基本上ViewModel。

var members = from m in db.Users where m.Id == UserID 
select new Member { FirstName = m.FirstName, 
        LastName = m.LastName, 
        Email = m.Email, 
        UserID = m.Id }; 

var member = members.FirstOrDefault(); 

然後,我可以在我的編輯視圖的頂部使用以下模型。

@model TestIdentity.Models.Member 
相關問題