以下是我得到的错误:
传递到字典中的模型项的类型为'System.Collections.Generic.List [StoredProcedureEF_MVC.tbl_users]',但此字典需要类型为'System.Collections.Generic.IEnumerable [StoredProcedureEF_MVC.Models]的模型项。用户]”。
我已经尝试了很多,但不知道我做错了什么,过去两天没有运气。我正在学习MVC,很抱歉,如果你有一些愚蠢的错误。
我的模特:
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; }
}
}
查看:
@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 })
)
)
)
控制器行动:
[HttpGet]
public ActionResult Delete()
{
SPEFMVCEntities conn = new SPEFMVCEntities();
var result = (from p in conn.tbl_users select p).ToList();
return View(result);
}
答案 0 :(得分:1)
尝试
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);
答案 1 :(得分:0)
尝试将控制器方法更改为:
[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);
}