我對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; }
}
}
您尚未實現對象。你所稱的成員實際上是一個返回成員對象的查詢。要獲取成員,請在查詢上調用.FirstOrDefault():var actualMember = member.FirstOrDefault()。 – dbugger