Элемент модели, передаваемый в словарь, имеет тип ' system.collections.generic.list`1[dempproject.models.user]', но для этого словаря требуется элемент модели типа 'dempproject.models.admin'.
У меня есть ошибка:
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Dempproject.Models.user]', but this dictionary requires a model item of type 'Dempproject.Models.admin'.
использование MVC 4 razor отображение записей с помощью webgrid. any one clear the error
Что я уже пробовал:
кодирование контроллера:
public ActionResult view() { List<user> lmd = new List<user>(); // creating list of model. DataSet ds = new DataSet(); Connection con = new Connection(); ds = con.mydata(); // fill dataset foreach (DataRow dr in ds.Tables[0].Rows) // loop for adding add from dataset to list<modeldata> { lmd.Add(new user { username = dr["Username"].ToString(),// adding data from dataset row in to list<modeldata> Userid = dr["Userid"].ToString(), email = dr["Email"].ToString(), Groupname = dr["groupName"].ToString(), customerno = dr["CustomerNo"].ToString(), Password_1 = dr["Password1"].ToString(), phoneno = dr["Phoneno"].ToString(), Faxno = dr["Faxno"].ToString() }); } return View(lmd.AsEnumerable()); }
просмотр кодирования:
@model IEnumerable<Dempproject.Models.user> @{ ViewBag.Title = "view"; WebGrid grid = new WebGrid(Model, rowsPerPage: 5); int rowval = 0; @*Layout = "~/Views/Shared/_AdminLayout.cshtml";*@ } <h2>view</h2> <style type="text/css"> .webGrid { margin: 4px; border-collapse: collapse; width: 100%; font-family: Tahoma; } .grid-header { background-color: #990000; font-weight: bold; color: White !important; } .webGrid th a { color: White; text-decoration: none; } .webGrid th, .webGrid td { border: 1px solid black; padding: 5px; } .alt { background-color: #F4EFEF; } .webGrid th a:hover { text-decoration: underline; } .to-the-right { text-align: right; } </style> <div id="gridcontent" style="padding-right:45%;"> @grid.GetHtml( tableStyle: "webGrid", fillEmptyRows: false, alternatingRowStyle: "alt", headerStyle: "grid-header", footerStyle: "foot-grid", mode: WebGridPagerModes.All, //paging to grid firstText: "<< First", previousText: "< Prev", nextText: "Next >", lastText: "Last >>", columns: new[] // colums in grid { grid.Column("Sr.No.", format: item => rowval = rowval + 1), grid.Column("Username"), //the model fields to display grid.Column("Userid"), grid.Column("Email"), grid.Column("groupName"), grid.Column("CustomerNo"), grid.Column("Password_1"), grid.Column("Phoneno"), grid.Column("Faxno"), grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.Userid })), grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.Userid })) }) </div>
модель connection. cs:
public DataSet mydata() { MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["Mycon"].ToString()); MySqlCommand cmd = new MySqlCommand("select * from dgv_user", con); cmd.CommandType = CommandType.Text; MySqlDataAdapter da = new MySqlDataAdapter(); da.SelectCommand = cmd; DataSet myrec = new DataSet(); da.Fill(myrec); return myrec; }
пользователей.в CS:
public class user { [Required(ErrorMessage = "userid is required")] // make the field required [Display(Name = "userid")] // Set the display name of the field public string Userid { get; set; } [Required(ErrorMessage = "Username is required")] // make the field required [Display(Name = "Username")] // Set the display name of the field public string username { get; set; } [Required(ErrorMessage = "Password is required")] [Display(Name = "Password")] public string password { get; set; } [Required(ErrorMessage = "Email is required")] [Display(Name = "Email")] [DataType(DataType.EmailAddress)] public string email { get; set; } [Required(ErrorMessage = "groupName is required")] [Display(Name = "groupName")] public string Groupname { get; set; } [Required(ErrorMessage = "CustomerNo is required")] [Display(Name = "CustomerNo")] public string customerno { get; set; } [Required(ErrorMessage = "Password1 is required")] [Display(Name = "Password1")] public string Password_1 { get; set; } [Required(ErrorMessage = "Phoneno is required")] [Display(Name = "Phoneno")] public string phoneno { get; set; } [Required(ErrorMessage = "Faxno is required")] [Display(Name = "Faxno")] public string Faxno { get; set; } public int Insert(string _userid, string _username, string _password,string _email,string _groupname,string _customerno,string _password1,string _phoneno,string _faxno) { string conname = ConfigurationManager.ConnectionStrings["Mycon"].ToString(); MySqlConnection cn = new MySqlConnection(conname); cn.Open(); MySqlCommand cmd = new MySqlCommand("Insert Into dgv_user(Userid,Username,Password,Email,groupName,CustomerNo,Password1,Phoneno,Faxno)Values('" + _userid + "','" + _username + "','" + _password + "','"+ _email +"','"+_groupname +"','"+ _customerno+"','"+_password1 +"','"+ _phoneno+"','"+_faxno +"')", cn); return cmd.ExecuteNonQuery(); cn.Close(); } }
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)
Вы уверены, что это именно тот вид, на который нацелены?