I am using ASP.NET MVC3 pattern with Razor view engine and trying to bind the values which is returned as DataTable from Model.
Model method:
public class SelectSiteModel
{
[Required]
public string SiteID { get; set; }
[Required]
public string SiteName { get; set; }
[Required]
public string SiteAddress { get; set; }
[Required]
public string WorkOrderNo { get; set; }
public IEnumerable
{
SqlConnection cn = new SqlConnection(@"Data Source=USHODAYA-PC1;Initial Catalog=VendorManagement;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * From Site", cn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds.Tables[0].AsEnumerable();
//return ConvertToListDictionary(ds.Tables[0]);
}
Controller Code:
public class SiteController : Controller
{
SqlConnection cn = new SqlConnection(@"Data Source=USHODAYA-PC1;Initial Catalog=VendorManagement;Integrated Security=True");
public ActionResult Index()
{
SelectSiteModel selectSiteModel = new SelectSiteModel();
var resultSet = selectSiteModel.GetAllSiteDetails();
return View(resultSet);
}
View Code:
@{
var grid = new WebGrid(source:Model,rowsPerPage: 3);
}
Employee List
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns(
grid.Column("SiteID"),
grid.Column("SiteName"),
grid.Column("SiteAddress"),
grid.Column("WorkOrderNo")
)
)
My above code still showing errors like
CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable
Source Error:
| Line 51: Line 52: @{ Line 53: var grid = new WebGrid(source:Model,rowsPerPage: 3); Line 54: } Line 55: Employee List |
Please solve my Problem
Regards
NagaRaju T

Jaganathan BantheswaranPosted Oct 23, 2013, 5:40 AM
Try this.
List
new SelectSiteModel
{
SiteID = row.Field
SiteName= row.Field
}).ToList();
Sachin RPosted Apr 11, 2015, 1:29 AM
getting output as below with no data displayed - BUT PAGINATION IN CORRECT - 3 PAGES
Sachin RPosted Apr 11, 2015, 1:13 AM
I have the similar kind of problem .Can u please help me to over come this.
Issue is column names are not recognized
source code
controller
model
view
Column "productname" does not exist.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: Column "productname" does not exist.
Source Error:
Jaganathan BantheswaranPosted Oct 24, 2013, 1:20 AM
NAGARAJU TAMADAPosted Oct 23, 2013, 11:53 PM
My Application working fine now
Regards
Nagaraju T
Jaganathan BantheswaranPosted Oct 23, 2013, 7:41 AM
@model Mvc3CssApp.Models.List
@model List
NAGARAJU TAMADAPosted Oct 23, 2013, 6:38 AM
@model Mvc3CssApp.Models.SelectSiteModel
but still there is no use
if i use this
@model Mvc3CssApp.Models.List
Exception raises like
The type or namespace name 'SelectSiteModel' could not be found (are you missing a using directive or an assembly reference?)
Jaganathan BantheswaranPosted Oct 23, 2013, 6:29 AM
Return type of your method should be List
&
Add @model List
NAGARAJU TAMADAPosted Oct 23, 2013, 6:21 AM
My Model is
My Controller is
View is
Employee List
Jaganathan BantheswaranPosted Oct 23, 2013, 6:03 AM
NAGARAJU TAMADAPosted Oct 23, 2013, 6:02 AM
Still the exception raises like,
The best overloaded method match for 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable
Line 52: @{ Line 53: var grid = new WebGrid(source:Model,rowsPerPage: 3);
Line 54: }
Line 55:
Employee List
NAGARAJU TAMADAPosted Oct 23, 2013, 4:01 AM
Again showing the error is
'object' does not contain a definition for 'Columns' and no extension method 'Columns' accepting a first argument of type 'object' could be found
Jaganathan BantheswaranPosted Oct 23, 2013, 3:02 AM
var source = new List
foreach(var row in resultSet.Table[0].Rows)
{
var site = new SelectSiteModel();
site.SiteName = row.Columns["SiteName"].ToString();
......
.....
source.Add(site);
}
return source;
NAGARAJU TAMADAPosted Oct 23, 2013, 2:46 AM
There is a error like
"Cannot apply indexing with [] to an expression of type 'object' "
var source = new List
foreach(var row in resultSet.Table[0].Rows)
{
var site = new SelectSiteModel();
site.SiteName = row["SiteName"].ToString(); //error occured here
......
.....
source.Add(site);
}
return source;
Jaganathan BantheswaranPosted Oct 23, 2013, 2:02 AM
you have to map the DataTable values with your SelectSiteModel.
Something like,
var source = new List
foreach(var row in resultSet.Table[0].Rows)
{
var site = new SelectSiteModel();
site.SiteName = row["SiteName"].ToString();
......
.....
source.Add(site);
}
return source;