Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have Error:
C#
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:
C#
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());

     }




view codeing:

CSS
@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>


model connection.cs:
C#
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:
C#
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();
        }


    }
Posted
Comments
Are you sure this is the view targeted?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900