NAGARAJU TAMADA

NAGARAJU TAMADA

  • NA
  • 205
  • 286.2k

DataSet binding to the WebGrid in asp.net mvc3

Oct 23 2013 1:39 AM
Hi,
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<object> GetAllSiteDetails()
        {
            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);
}
<h2>Employee List</h2>


<div id="grid">
    @grid.GetHtml(
        tableStyle: "grid",
        headerStyle: "head",
        alternatingRowStyle: "alt",
        columns: grid.Columns(
            grid.Column("SiteID"),
            grid.Column("SiteName"),
            grid.Column("SiteAddress"),
grid.Column("WorkOrderNo")


        )
    )
</div>
My above code still showing errors like
CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable<dynamic>, System.Collections.Generic.IEnumerable<string>, string, int, bool, bool, string, string, string, string, string, string, string)' has some invalid arguments

Source Error:


Line 51: Line 52:
@{ Line 53: var grid = new WebGrid(source:Model,rowsPerPage: 3);

Line 54: }
Line 55: <h2>Employee List</h2>

Please solve my Problem

Regards
NagaRaju T





Answers (15)