Grid doent load/bind data from the controller

Hello I am new to MVC and Syncfusion, The grid doesnt seem retrieve data fro the controller I am using EJS2 with MVC 5.

This cotroller i generated from ADO.NET model throught EF

namespace Plastogram_System.Controllers
{
    public class ProductsController : Controller
    {
        public PlastogramSystemDBEntitiesALL99 db = new PlastogramSystemDBEntitiesALL99();
     
        // GET: Products
        public ActionResult Index()
        {
            var datasource = db.Products.Include(p => p.Location).Include(p => p.Product_Location).Include(p => p.Product_Quote).Include(p => p.Product_Type).Include(p => p.Product_Price);
            ViewBag.DataSource = datasource.ToList();
            return View(ViewBag.DataSource);
        }

   

        // GET: Products/Create
        public ActionResult Create()
        {
            ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationDescription");
            ViewBag.ProductID = new SelectList(db.Product_Location, "ProductID", "ProductID");
            ViewBag.ProductID = new SelectList(db.Product_Quote, "ProductID", "ProductID");
            ViewBag.ProductTypeID = new SelectList(db.Product_Type, "ProductTypeID", "ProductTypeDescription");
            ViewBag.PriceID = new SelectList(db.Product_Price, "PriceID", "PriceID");
            return View();
        }

        // POST: Products/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [System.Web.Http.HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ProductID,ProductName,ProductColour,ProductTypeID,ProductQuantity,ProductBarcode,ProductPrice,Unitsize,ProductSize,LocationID,PriceID")] Product product)
        {
            if (ModelState.IsValid)
            {
                db.Products.Add(product);
                db.SaveChanges();
                return RedirectToAction("Create");
            }

            ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationDescription", product.LocationID);
            ViewBag.ProductID = new SelectList(db.Product_Location, "ProductID", "ProductID", product.ProductID);
            ViewBag.ProductID = new SelectList(db.Product_Quote, "ProductID", "ProductID", product.ProductID);
            ViewBag.ProductTypeID = new SelectList(db.Product_Type, "ProductTypeID", "ProductTypeDescription", product.ProductTypeID);
            ViewBag.PriceID = new SelectList(db.Product_Price, "PriceID", "PriceID", product.PriceID);
            return View(product);
        }


        // GET: Products/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        // POST: Products/Delete/5
        [System.Web.Http.HttpPost, System.Web.Http.ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Product product = db.Products.Find(id);
            db.Products.Remove(product);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}


This my view


@model IEnumerable<Plastogram_System.Models.Product>


@Html.EJS().Grid("CommandColumn").DataSource(dataManager => { dataManager.Url("/Products/Index").Adaptor("UrlAdaptor"); }).Columns(col =>
{
    col.Field("ProductID").HeaderText("ProductID").IsPrimaryKey(true).Width("120").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("ProductName").HeaderText("ProductName").Width("150").Add();
    col.Field("ProductQuantity").HeaderText("ProductQuantity").Width("120").Format("C2").TextAlign(Syncfusion.EJ2.Grids.TextAlign.Right).Add();
    col.Field("LocationDescription").HeaderText("LocationDescription").EditType("dropdownedit").Width("150").Add();
}).AllowPaging().AllowSorting().AllowFiltering().Toolbar(new List<string>() { "Search" }).Render()



I have tried lot of the binding options at https://ej2.syncfusion.com/aspnetmvc/documentation/grid/databinding.html
and these is the one that displays the table loading but it doesnt show data inside it. Please help i had the same problem with Kendo as well.

















1 Reply

HJ Hariharan J V Syncfusion Team September 10, 2018 07:06 AM UTC

Hi Urt, 

Thanks for contacting Syncfusion Support 

We have checked your query and In Essential JavaScript 2 Grid, the data should be returned as result and count pair while using UrlAdaptor. This is the default behavior. In your sample you have returned ViewBag data only. This is the cause of the reported behavior.  We have prepared a simple sample based on your requirement. Please refer to the bellow code example, Demo link and sample link.  

[index.cshtml] 
@Html.EJS().Grid("Grid").DataSource(dataManager => { dataManager.Url("/Home/UrlDatasource").Adaptor("UrlAdaptor"); }).Columns(col => 
       { 
           col.Field("OrderID").HeaderText("Order ID").Width("120").Add(); 
           .    .   . 
       }).AllowPaging().Render() 
 


[Controller.cs] 
public class HomeController : Controller 
    { 
 
        public static List<OrdersDetails> orddata = new List<OrdersDetails>(); 
        .  .  . 
 
        public ActionResult UrlDatasource(DataManagerRequest dm) 
        { 
            IEnumerable DataSource = orddata.ToList(); 
            DataOperations operation = new DataOperations(); 
 
            if (dm.Search != null && dm.Search.Count > 0) 
            { 
                DataSource = operation.PerformSearching(DataSource, dm.Search);  //Search 
            } 
            if (dm.Sorted != null && dm.Sorted.Count > 0) //Sorting 
            { 
                DataSource = operation.PerformSorting(DataSource, dm.Sorted); 
            } 
            if (dm.Where != null && dm.Where.Count > 0) //Filtering 
            { 
                DataSource = operation.PerformFiltering(DataSource, dm.Where, dm.Where[0].Operator); 
            } 
            int count = DataSource.Cast<OrdersDetails>().Count(); 
            if (dm.Skip != 0) 
            { 
                DataSource = operation.PerformSkip(DataSource, dm.Skip);   //Paging 
            } 
            if (dm.Take != 0) 
            { 
                DataSource = operation.PerformTake(DataSource, dm.Take); 
            } 
             
            return dm.RequiresCounts ? Json(new { result = DataSource, count = count }) : Json(DataSource); 
        } 
} 

Demo                 : https://aspnetmvc.syncfusion.com/Grid/UrlAdaptor#/material  


Regards, 
Hariharan 


Loader.
Up arrow icon