1

Below is the error i getting :

The model item passed into the dictionary is of type 'System.Collections.Generic.List[StoredProcedureEF_MVC.tbl_users]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable[StoredProcedureEF_MVC.Models.User]'.

I have tried a lot but don't know where i was doing wrong, no luck from last two days. I am learning MVC, so sorry if you got some stupid mistake.

My Model :

namespace StoredProcedureEF_MVC.Models { [Table("tbl_users")] public class User { [Key] public int UserId { get; set; } [Required] public string Username {get;set;} [Required] public string Email { get; set; } } } 

VIEW:

@model IEnumerable<StoredProcedureEF_MVC.Models.User> @{ ViewBag.Title = "Delete"; } @{ var grid = new WebGrid(source: Model); } @grid.GetHtml( columns: grid.Columns( grid.Column("UserID"), grid.Column("Username"), grid.Column("Email"), grid.Column( format: (item) => Html.ActionLink("Delete", "DeleteUser", new {id=item.UserID }) ) ) ) 

CONTROLLER ACTION:

[HttpGet] public ActionResult Delete() { SPEFMVCEntities conn = new SPEFMVCEntities(); var result = (from p in conn.tbl_users select p).ToList(); return View(result); } 
6
  • Well presumably conn.tbl_users is declared to return an IQueryable<StoredProcedureEF_MVC.tbl_users>, not an StoredProcedureEF_MVC.Models.User... How did you expect the conversion to be performed from one to the other? Commented Mar 20, 2015 at 6:57
  • StoredProcedureEF_MVC.tbl_users is not the same type as StoredProcedureEF_MVC.Models.User Commented Mar 20, 2015 at 6:58
  • @Jon Skeet, so what's the solution ? Commented Mar 20, 2015 at 7:01
  • Potentially write a projection from one type to the other. Potentially change to only have one type at all. We don't know enough about your system to say. Do you understand the error though? Commented Mar 20, 2015 at 7:05
  • @Jon Skeet, thanks for your help, found solution. Commented Mar 20, 2015 at 7:08

2 Answers 2

1

try

var result = conn.tbl_users.Select(c => new StoredProcedureEF_MVC.Models.User { UserId = c.UserId, UserName = c.UserName, Email = c.Email }).ToList(); return View(result); 
Sign up to request clarification or add additional context in comments.

2 Comments

This will work for sure, but I don't think that's the right way to do this.
Not even "for sure" - you may need an AsEnumerable in there (or an intermediate projection to an anonymous type, followed by AsEnumerable, followed by this projection) as otherwise the LINQ provider may complain that it doesn't "know" about Models.User.
0

Try to change your controller method to this:

[HttpGet] public ActionResult Delete() { SPEFMVCEntities conn = new SPEFMVCEntities(); var result = (from p in conn.tbl_users select p as StoredProcedureEF_MVC.Models.User).ToList(); return View(result); } 

2 Comments

NO LUCK : ERROR ---Unable to cast the type 'StoredProcedureEF_MVC.tbl_users' to type 'StoredProcedureEF_MVC.Models.User'. LINQ to Entities only supports casting EDM primitive or enumeration types.
And have you tried changing conn.tbl_users to conn.User or conn.Users? Does that work?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.