Click here to Skip to main content
16,004,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I a trying to pass data table values to the webgrid in view, but browser shows an exception like,
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[NewAuthentication.Models.DataModel]'.

What I have tried:

my view code is...

@model IEnumerable<newauthentication.models.datamodel>

@{
ViewBag.Title = "myprof";
Layout = "~/Views/Shared/UserMaster.cshtml";
WebGrid grid = new WebGrid(Model);
}

myprof


@grid.GetHtml( columns:new[] { grid.Column("Firstname"),
grid.Column("Gender")
})

and server side code is below...

SqlConnection con = new SqlConnection("Data Source=JAGDISH;Database=Hmarasmaj;Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("Select * from Usercredentials where id="+1+"",con);
DataTable dt = new DataTable();
da.Fill(dt);
DataModel dm = new DataModel();
int count = dt.Rows.Count;
List<string> list = new List<string>();

    list.Add(dt.Rows[0]["Firstname"].ToString());
list.Add(dt.Rows[0]["Gender"].ToString());
return View(list);
Posted
Updated 15-Jul-17 6:44am
v2

1 solution

You might be overthinking it but it is clear from your server side code that you are passing into the view list variable which is of type List<string>.

You need to change

C#
return View(list);


to

C#
return View(dm);


Where dm is your DataModel class which is what, at least from your question, your view requires to be the model.
 
Share this answer
 

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



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