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 BantheswaranRank #170 · 14.1K reputationPosted Oct 23, 2013, 1:40 AM EDT
Try this.
List
new SelectSiteModel
{
SiteID = row.Field
SiteName= row.Field
}).ToList();
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 22, 2013, 10:02 PM EDT
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;
NAGARAJU TAMADA134 reputationPosted Oct 22, 2013, 10:46 PM EDT
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 BantheswaranRank #170 · 14.1K reputationPosted Oct 22, 2013, 11:02 PM EDT
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 TAMADA134 reputationPosted Oct 23, 2013, 12:01 AM EDT
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
NAGARAJU TAMADA134 reputationPosted Oct 23, 2013, 2:02 AM EDT
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
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 23, 2013, 2:03 AM EDT
NAGARAJU TAMADA134 reputationPosted Oct 23, 2013, 2:21 AM EDT
My Model is
My Controller is
View is
Employee List
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 23, 2013, 2:29 AM EDT
Return type of your method should be List
&
Add @model List
NAGARAJU TAMADA134 reputationPosted Oct 23, 2013, 2:38 AM EDT
@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 BantheswaranRank #170 · 14.1K reputationPosted Oct 23, 2013, 3:41 AM EDT
@model Mvc3CssApp.Models.List
@model List
NAGARAJU TAMADA134 reputationPosted Oct 23, 2013, 7:53 PM EDT
My Application working fine now
Regards
Nagaraju T
Jaganathan BantheswaranRank #170 · 14.1K reputationPosted Oct 23, 2013, 9:20 PM EDT
Sachin R82 reputationPosted Apr 10, 2015, 9:13 PM EDT
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:
Sachin R82 reputationPosted Apr 10, 2015, 9:29 PM EDT
getting output as below with no data displayed - BUT PAGINATION IN CORRECT - 3 PAGES
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts