0

I'm fairly new with MVC and completely new with Identity 2.0. I'm trying to create a way to Edit a user and I'm not having much luck. Ultimately I would like to use a POCO class I created called Member so that I can shape the way the data will be passed to the View. However, now I can't even get it to display using the ApplicationUser from Identity. I had it working with the ApplicationUser at one point but then when I tried changing it to a Member it didn't work and now I can't get it to revert back to the ApplicationUser.

I've searched all over for a sample of doing CRUD on Identity 2.0 but haven't found anything that made any kind of sense. So, now I'm using the sample code from Microsoft's Contoso University project and trying to tweak it to work. I can get the list of users in a Grid.Mvc with no problem.

Any help would be greatly appreciated.

This is the error I'm getting in the View:

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery`1[TestIdentity.Models.ApplicationUser]', but this dictionary requires a 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 = 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);            
        }               

    }

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

Here is my Member.cs class I would like to 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; }
    }
}
1
  • 1
    You haven't materialized the object yet. What you have called member is really a query to return member objects. To get the member, call .FirstOrDefault() on the query: var actualMember = member.FirstOrDefault(). Commented Sep 26, 2015 at 3:03

2 Answers 2

1

Your member is IQueryable<ApplicationUser>, not the ApplicationUser object

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

There are two ways to fix it:

var members = from m in db.Users
             where 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.
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. The lambda expression worked. However, for learning purposes I was trying to get it to convert over to my Member.cs so I could shape the data some.
I've tried a couple things like var members = from m in db.Users where m.Id == UserID select m; with this var member = from m in members select new Member { FirstName = m.FirstName, LastName = m.LastName, Email = m.Email, UserID = m.Id };
also tried something similar but using Member member= from m in members select new Member { FirstName = m.FirstName, LastName = m.LastName, Email = m.Email, UserID = m.Id }; with no luck.
Is it possible to covert it to my Member class? If not, anyone have a quick explanation why so I can understand more about what I'm doing.
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(); Hi, can you try the code above?
0

Perfect! Thanks to kienct89.

This is what I ended up using to get the User and then map them to basically a 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();

Then I was able to use the following as the model at the top of my Edit View.

@model TestIdentity.Models.Member

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.