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