public ActionResult WebGrid() { ProductModel model = new ProductModel(); PublicUserCSVModel csvModel = new PublicUserCSVModel(); if (TempData.ContainsKey("PublicUpload")) { csvModel = (PublicUserCSVModel)TempData["PublicUpload"]; //latest uploaded csv Session["PublicUpload"] = csvModel; } if (Session.Keys.Equals("PublicUpload")) { List <Product> products = new List <Product>(); using (var entities = new ApplicationDbContext()) { //entities.UserCSVDatabase.Where(r => r.Id >= 1).Select(r => products.Add(r)); var prod = from p in entities.UserCSVDatabase select p; products = prod.ToList(); } if (products != null && products.Count > 0) { model.CreateGrid(products); } } return(View(model)); }
public ActionResult PublicUpload(HttpPostedFileBase upload) { if (ModelState.IsValid) { if (upload != null && upload.ContentLength > 0) { if (upload.FileName.EndsWith(".csv")) { Stream stream = upload.InputStream; DataTable csvDataTable = new DataTable(); PublicUserCSVModel model = new PublicUserCSVModel(); model.Name = upload.FileName; using (CsvReader csvReader = new CsvReader(new StreamReader(stream), true)) { csvDataTable.Load(csvReader); } string[] csvColNames = csvDataTable.Columns.Cast <DataColumn>() .Select(x => x.ColumnName) .ToArray(); model.ColName = csvColNames.ToList(); // Populating PublicUserCSVModel var table = new List <List <string> >(); // each list is a col with [0]=name of col foreach (var colName in csvColNames) { string[] ColItems = csvDataTable.AsEnumerable() .Select(s => s.Field <string>(colName)).ToArray <string>(); var ret = ColItems.ToList(); ret.Insert(0, colName); table.Add(ret); } model.Table = table; // var csvGrid = new MVCGridConfig(); //csvGrid.Model = model; // set model in grid class //var works = DataYachtz.MVCGridConfig.Model.GetList(2); // csvGrid.RegisterGrids(); //DataYachtz.MVCGridConfig.Model = model; //var grid = new MVCGridToolbarModel(model.Name); TempData["PublicUpload"] = model; return(RedirectToAction("WebGrid")); } else { ModelState.AddModelError("File", "This file format is not supported"); return(View()); } } else { ModelState.AddModelError("File", "Please Upload Your file"); } } return(View()); }