Vishnu Sharma
Vishnu Sharma

Reputation: 89

How can i delete a row from table without refreshing the Page?

How can i delete a row from table without refreshing the Page? I deleted the delete view from the views but it is showing me "The Resources Cannot Be Found" I am not so perfect in MVC if someone could help me with this,that will be so thankful.

Here is my Controller

[Here is my Index view

<table id="customers">
    <tr>
        <th style="text-align:center">
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th style="text-align:center">
            @Html.DisplayNameFor(model => model.Salary)
        </th>
        <th style="text-align:center">
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th style="text-align:center">Action</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="centerAlign">
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Salary)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>

public ActionResult Delete(int id = 0)
        {
            New_Table new_table = db.New_Table.Find(id);
            if (new_table == null)
            {
                return HttpNotFound();
            }
            return View(new_table);
        }

        //
         //POST: /Default1/Delete/5

        [HttpPost]
        public ActionResult Delete(int id)
        {
            New_Table new_table = db.New_Table.FirstOrDefault(s => s.Id.Equals(id));
            if (new_table != null)
            {
                db.DeleteObject(new_table);
                db.SaveChanges();
            }
            return View("Index");
        }

]2

Upvotes: 1

Views: 1708

Answers (2)

Sahil Sharma
Sahil Sharma

Reputation: 1899

Simply use JavaScript and Ajax for it. Try this approach in your DeleteView:

<script>
function DeleteId(id)
{
  $.ajax({
     dataType: "json",
     url: '@Url.Action("ActionName","ControllerName")',
     data: {id: id},
     success: function(result){
                //todo
            }
       });
    }
</script>

<table id="customers">
    <tr>
        <th style="text-align:center">
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th style="text-align:center">
            @Html.DisplayNameFor(model => model.Salary)
        </th>
        <th style="text-align:center">
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th style="text-align:center">Action</th>
    </tr>

    @foreach (var item in Model)
    {
        <tr class="centerAlign">
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Salary)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>

            <!-- Code Removed for simplicity -->
            <a onClick="DeleteId('@item.Id');"> Delete <a/>
            </td>
        </tr>

Upvotes: 0

Sombir Kumar
Sombir Kumar

Reputation: 1881

  1. Remove Action Link:- @Html.ActionLink("Edit", "Edit", new { id = item.Id })
  2. Put this item.id in hidden field
  3. Get the id from hidden field in a javascript variable.
  4. Call your delete action method via ajax[POST] call, pass id in ajax call.

Hope it will help!

Upvotes: 1

Related Questions