The below code throws error in IEnumerable conversion issues. Please guide me if I am mising some conversion from controller to view.
controller Code
public ActionResult Index()
{
DataClasses1DataContext db = new DataClasses1DataContext();
var kk = (from x in db.tblTests select x).ToList();
return View(kk);
}
View Code
@model MvcApplication4.Models.sampleDoamin
@{ ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@{ var grid = new WebGrid(Model, defaultSort:"name"); }
@grid.GetHtml()
That is because the action method is passing a list to the view. The way you had it before, the view was expecting a single sampleDoamin, not a collection.
I tried with IEnumerable in view and also with List in View but it throws below error ...
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication4.tblTest]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[MvcApplication4.Models.sampleDoamin]'.
Sorry I didn't notice that. Yes, the problem is that in your action method you are returning a List of tblTest, but in your view you are accessing a List of sampleDoamin.
chandrasheka...
Member
5 Points
47 Posts
webgrid issue
May 07, 2012 07:15 PM|LINK
The below code throws error in IEnumerable conversion issues. Please guide me if I am mising some conversion from controller to view.
controller Code
public ActionResult Index() { DataClasses1DataContext db = new DataClasses1DataContext(); var kk = (from x in db.tblTests select x).ToList(); return View(kk); }View Code
@model MvcApplication4.Models.sampleDoamin @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> @{ var grid = new WebGrid(Model, defaultSort:"name"); } @grid.GetHtml()Model Code
CodeHobo
All-Star
18647 Points
2647 Posts
Re: webgrid issue
May 07, 2012 08:24 PM|LINK
Your model decleration in the view is incorrect. It needs to be
That is because the action method is passing a list to the view. The way you had it before, the view was expecting a single sampleDoamin, not a collection.
Blog | Twitter : @Hattan
chandrasheka...
Member
5 Points
47 Posts
Re: webgrid issue
May 07, 2012 10:38 PM|LINK
CodeHobo,
I tried with IEnumerable in view and also with List in View but it throws below error ...
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication4.tblTest]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[MvcApplication4.Models.sampleDoamin]'.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: webgrid issue
May 07, 2012 11:17 PM|LINK
Sorry I didn't notice that. Yes, the problem is that in your action method you are returning a List of tblTest, but in your view you are accessing a List of sampleDoamin.
To fix this change your view to
Blog | Twitter : @Hattan
chandrasheka...
Member
5 Points
47 Posts
Re: webgrid issue
May 07, 2012 11:21 PM|LINK
Thanks for inclining me towards right path.