i'm new to mvc and trying to get idea of what i'm doing wrong i create a Dbcontaxt
class
public class DataBaseContext : DbContext
{
public DataBaseContext()
: base("DefaultConnection")
{
}
public DbSet<Membership> Membership { get; set; }
public DbSet<OAuthMembership> OAuthMembership { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Category> Categorys { get; set; }
public DbSet<SubCategory> SubCategorys { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Color> Colors { get; set; }
public DbSet<Size> Sizes { get; set; }
public DbSet<Company> Companys { get; set; }
public DbSet<UsersInRoles> UsersInRoles { get; set; }
}
}
and i create a model class to create a strongly type view
[Bind(Exclude = "AddUserToRoleID")]
public class AddUserToRole
{
[ScaffoldColumn(false)]
public int AddUserToRoleID { get; set; }
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[Display(Name = "Role name")]
public string RoleName { get; set; }
}
}
in the controller i'm trying to create the Details view by adding view and select AddUserToRole
as my model for the strongly type view
public ActionResult Details(int id = 0)
{
var UserInRole = db.UserProfiles
.Join(db.UsersInRoles,
u => u.UserId,
uir => uir.UserId,
(u, uir) => new {u = u,uir = uir})
.Join(db.Roles,
temp0 => temp0.uir.RoleId,
r => r.RoleId,
(temp0, r) => new { temp0 = temp0,r = r })
.Where(temp1 => (temp1.temp0.u.UserId == id))
.Select(temp1 => new AddUserToRole {
AddUserToRoleID = temp1.temp0.u.UserId,
UserName = temp1.temp0.u.UserName,
RoleName = temp1.r.RoleName
});
return View(UserInRole);
}
it give me this error
The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[SeniorProject.Models.AddUserToRole]', but
this dictionary requires a model item of type 'SeniorProject.Models.AddUserToRole'.
and when i cast return View((UsersInRoles)UserInRole);
it give me this error
Unable to cast object of type
'System.Data.Entity.Infrastructure.DbQuery`1[SeniorProject.Models.AddUserToRole]'
to type'SeniorProject.Models.UsersInRoles'.
and the view
@model SeniorProject.Models.AddUserToRole
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<fieldset>
<legend>AddUserToRole</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.UserName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.UserName)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.RoleName)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.RoleName)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { id=Model.AddUserToRoleID }) |
@Html.ActionLink("Back to List", "Index")
</p>
what should i do in this case?
You need to materialize it. Put FirstOrDefault() at the end of the query
var materializedUser = UserInRole.SingleOrDefault();
return View(materializedUser);
Edit: Following pjotr comment replacing FirstOrDefault() with SingleOrDefault()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With