如何从Asp.Net Identity 2获取用户?

时间:2015-09-26 02:56:42

标签: c# asp.net asp.net-mvc asp.net-identity-2

我对MVC很新,对Identity 2.0来说是全新的。我正在尝试创建一种编辑用户的方法,但我没有太多运气。最后,我想使用我创建的名为Member的POCO类,以便我可以塑造将数据传递给View的方式。但是,现在我甚至无法使用Identity中的ApplicationUser进行显示。我曾经在ApplicationUser上使用它,但是当我尝试将它更改为一个成员时它没有用,现在我无法让它恢复到ApplicationUser。

我搜索了一下在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; }
    }
}

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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