troubleshooting Question

Single/Multiple row delete in WebGrid MVC

Avatar of PratikShah111
PratikShah111 asked on
C#ASP.NET.NET MVC
1 Comment1 Solution36 ViewsLast Modified:
I am working on a file uploader tool in MVC and once the files are uploaded I want to show all the files that are uploaded in a Webgrid.
In the webgrid I want the user to have the ability to delete one file from the grid or select all files and delete them. so when they delete it the files needs to be removed from its physical location.



Here is my controller code

public ActionResult UploadFiles(HttpPostedFileBase[] files)
        {
            
            Models.FileModel f = new Models.FileModel();
            List<Models.FileModel> TableName = new List<Models.FileModel>();
            //Ensure model state is valid
            if (ModelState.IsValid)
            {   //iterating through multiple file collection 
                foreach (HttpPostedFileBase file in files)
                {
                    //Checking file is available to save.
                    if (file != null)
                    {
                        f.FileName = file.FileName;
                        long size = file.ContentLength / 1024 <= 0 ? 1 : file.ContentLength / 1024;
                        f.FileSize = string.Format("{0:N0} KB", size);
                        f.FileType = "File";
                        f.LastUpdate = DateTime.Now.ToShortDateString();
                        TableName.Add(f);
                        var InputFileName = Path.GetFileName(file.FileName);
                        var ServerSavePath = Path.Combine(Server.MapPath("~/UploadedFiles/") + InputFileName);
                        //Save file to server folder
                        file.SaveAs(ServerSavePath);
                        //assigning file uploaded status to ViewBag for showing message to user.
                        ViewBag.UploadStatus = files.Count().ToString() + " files uploaded successfully.";
                        
                    }


                }

              
               
               
                ViewBag.Message = TableName;
            }


            return View();
        }


Here is my View Code

@model UploadMultipleFilesInMVC.Models.FileModel
@{
    ViewBag.Title = "Upload Multiple Files";
    WebGrid grid = new WebGrid(ViewBag.Message);
}

 @if (ViewBag.UploadStatus != null)
            {
                @grid.GetHtml(
                    tableStyle: "table", // applying style on grid
                    fillEmptyRows: true,
                    //show empty row when there is only one record on page to it will display all empty rows there.
                    headerStyle: "header", //applying style.
                    footerStyle: "grid-footer", //applying style.

                    columns: new[]  // colums in grid
                    {
                        grid.Column("FileType"), //the model fields to display
                        grid.Column("FileName"  ),
                        grid.Column("LastUpdate"),
                        grid.Column("FileSize"),
                        grid.Column("ActualSize"),
                        grid.Column("ActualModifiedDate"),
                   })
            }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 1 Comment.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros