I have Error:
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'.
using mvc 4 razor display the records using webgrid .any one clear the error
What I have tried:
controller codeing:
public ActionResult view()
{
List<user> lmd = new List<user>();
DataSet ds = new DataSet();
Connection con = new Connection();
ds = con.mydata();
foreach (DataRow dr in ds.Tables[0].Rows)
{
lmd.Add(new user
{
username = dr["Username"].ToString(),
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());
}
view codeing:
@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,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[]
{
grid.Column("Sr.No.", format: item => rowval = rowval + 1),
grid.Column("Username"),
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>
model 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;
}
user.cs:
public class user
{
[Required(ErrorMessage = "userid is required")]
[Display(Name = "userid")]
public string Userid { get; set; }
[Required(ErrorMessage = "Username is required")]
[Display(Name = "Username")]
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();
}
}