asp.net mvc 4 - model passed to dictionary is of type .Data.Entity.Infrastructure.DbQuery , but it requires a model of type 'System.Collections.Generic.IEnumerable -
what trying
userroles:
public class userrolescontroller : controller { private hmsentities db = new hmsentities(); // // get: /userroles/ public actionresult index() { user user = (user)session["user"]; var usr = db.users.find(user.id); viewbag.id = usr.id; viewbag.firstname = usr.firstname; if (session["user"] != null) { var role = db.roles.where(u => u.id == user.roleid); } return view(usr); }
index.cshtml
@model ienumerable<hms.models.user> @using hms.models;
in project when user logs in user can view details , perform crud operations on roles of user, getting error:
the model item passed dictionary of type 'system.data.entity.infrastructure.dbquery
1[hms.models.role]', dictionary requires model item of type 'system.collections.generic.ienumerable
1[hms.models.user]'.
my models are:
role.cs
public partial class role { public role() { this.users = new hashset<user>(); } public int id { get; set; } public string name { get; set; } public virtual icollection<user> users { get; set; } }
user.cs
public partial class user { public user() { this.accesses = new hashset<access>(); this.doctors = new hashset<doctor>(); this.patients = new hashset<patient>(); this.staffs = new hashset<staff>(); } public int id { get; set; } public string firstname { get; set; } public string lastname { get; set; } public nullable<system.datetime> dob { get; set; } public nullable<int> age { get; set; } public nullable<int> phoneno { get; set; } public nullable<int> landlineno { get; set; } public nullable<bool> status { get; set; } public string permentaddress { get; set; } public string temproryaddress { get; set; } public string bloodgroup { get; set; } public string gender { get; set; } public string educationfinal { get; set; } public string experience { get; set; } public string emailid { get; set; } public byte[] image { get; set; } public string username { get; set; } public string password { get; set; } public nullable<int> roleid { get; set; } public virtual icollection<access> accesses { get; set; } public virtual icollection<doctor> doctors { get; set; } public virtual icollection<patient> patients { get; set; } public virtual role role { get; set; } public virtual icollection<staff> staffs { get; set; } }
i new mvc , don't know else do. if further code required please tell , please reply.
db.roles.where(u => u.id == user.roleid);
returns dbquery:
you have convert list or enumerable.
viewbag.role = db.roles.where(u => u.id == user.roleid).asenumerable(); return view(viewbag.role);
or if use list:
viewbag.role = db.roles.where(u => u.id == user.roleid).tolist();
and don't need put in viewbag:
var roles = db.roles.where(u => u.id == user.roleid).asenumerable(); return view(roles);
and in view change model user roles:
@model ienumerable<hms.models.user>
to roles
@model ienumerable<hms.models.roles>
edit:
you returning enumerable of roles on line: if (usr != null) return view(viewbag.role);
not user.
if want return user , roles, have include roles in user model.
something in controller:
var usr = db.users.find(user.id); usr.roles = db.roles.where(u => u.id == user.roleid).tolist(); return view(usr);
then in view:
@model hms.models.user @foreach(var role in model.roles){ <p>@role.roleid</p> }
Comments
Post a Comment