2

I created a functionality to delete a specific row in my webgrid table. This works fine but when the row is deleted I want my view to get refreshed and shows the actual data(after deleting a row).

This is what I got so far:

Webgrid view including link-tag that calls my controller and sends parameter ID with it.

<div class="col-lg-12 d-flex align-items-stretch">
            @grid.Table(tableStyle: "table table-responsive table-striped table-bordered",
                columns: grid.Columns(
                  grid.Column(columnName: "ApiRedirectID", header: "ID", format:@<text><div class="" data-id="@item.ApiRedirectID" data-propertyname="ApiRedirectID">@item.ApiRedirectID</div></text>),
                  grid.Column(columnName: "ApiName", header: "Name", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="ApiName">@item.ApiName</div></text>),
                  grid.Column(columnName: "Company.CompanyName", header: "Company Name", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="Company.CompanyName">@item.Company.CompanyName</div></text>),
                  grid.Column(columnName: "ApiURL2", header: "URL", format:@<text><div class="edit" data-id="@item.ApiRedirectID" data-propertyname="ApiURL2">@item.ApiURL2</div></text>),
                  grid.Column(columnName: "Delete", header: " ", format:@<a href="DeleteRow/@item.ApiRedirectID" class="display delete-btn"><span class="glyphicon glyphicon-remove" style="color:red;"></span></a>)
          )
     )

</div>

Controller that handles the deleting part:

public ActionResult DeleteRow(int id)
    {
        var status = false;
        var message = "";

        using (ConcurrentDBEntities db = new ConcurrentDBEntities())
        {
            var data = db.ApiRedirects.Find(id);
            if (data != null)
            {
                db.ApiRedirects.Remove(data);
                db.SaveChanges();
                status = true;
            }
            else
            {
                message = "Error!";
            }
            var response = new { id = id, status = status, message = message };
            JObject o = JObject.FromObject(response);
            return Content(o.ToString());
   }

This code works fine but when I delete a row the following is getting returned:

enter image description here

Instead of this I just want my view with my updated webgrid table getting returned.

Hope anyone can help!

NielsStenden
  • 357
  • 1
  • 6
  • 25

2 Answers2

2

Instead of

return Content(o.ToString());

Please use (If your action to list all objects is called Index):

return RedirectToAction("Index", new { id = id, status = status, message = message });

And in your Index action create parameters:

public ActionResult Index(int id = 0, int status = 0, string message = "") {}

You can use autoscaffolding build in Visual Studio to do CRUD automatically and then just customize it.

Jakub G
  • 189
  • 1
  • 9
  • This does not work, I'm getting the following error: "The view 'DeleteRow' or its master was not found or no view engine supports the searched locations. " – NielsStenden May 08 '19 at 12:14
1
JObject o = JObject.FromObject(response);
return Content(o.ToString());

In stead of returning a Json object you should return a view. Json is just a collection with data.

Example : return View("~/Views/SpecificView.cshtml")

You should give the data with that specificViewModel --> the model of the view

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
Tim Vd
  • 59
  • 13