0

I have an interesting problem while reloading partial views with ajax. I have a following setup in the master View:

<div>
    <div id="items">
        @Html.Partial("SubView", Model.Items);
    </div>
<div>

SubView is generally a list of items in a table as follows:

<table class="table table-striped">
    <tr>
        <th>Date</th>
        <th>Time</th>
        <th></th>
</tr>
@foreach (var item in Model)
{
    <tr>
        @Html.HiddenFor(modelItem => item.Id)
        <td>@Html.DisplayFor(modelItem => item.Date)</td>
        <td>@Html.DisplayFor(modelItem => item.Time)</td>
        <td>
           @Html.ActionLink("Delete", "Delete", new { itemId= item.Id, page = Model.PageNumber }, new { @class = "deleteItem" })
    </td>
</tr>
}

DeleteItem Action in the controller does basically the following:

    [HttpDelete]
    public ActionResult DeleteItem(int itemId, int page)
    {
        this.dbService.DeletItem(expenseId);
        return PartialView("SubView", this.GetPagedList(page));
    }

In the script that I refer in the master View I have the following code:

$(document).ready(function () {
// delete expense
$(".deleteItem").click(function () {

    $.ajax({
        url: this.href,
        type: 'delete',
        success: function (result) {
            $("#items").html(result);
        }
    });

    return false;
});

This works fine the first time, but the second time it only loads the partial View -> since the JavaScript code is not being executed...

I am relatively new to that stuff and I am a bit confused what's going on here. All 3rd party scripts are rendered in the Layout.cshtml

2
  • Please post you DeleteItem complete code as well Commented Aug 22, 2016 at 17:11
  • added, as I said it is very simple Commented Aug 22, 2016 at 17:19

2 Answers 2

1

You can't attach a .click() event to a dynamically generated item. You have to structure it this way:

$(document).on('click', '.deleteItem', function() {
    // Deletey stuff here.
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's the missing piece. The same code as I posted worked in a previous project, but there I have the script directly in my master view - that's probably why it worked.
It's because dynamically created items aren't part of the DOM when the document is first loaded, which is when the binding occurs. The above code attaches the .click() event to the document instead.
0

For partial views to render, you have to make the return type PartialViewResult rather than ActionResult. Because ActionResult causes a redirection.

Try editing your DeleteItem function like this.

[HttpDelete]
    public PartialViewResult DeleteItem(int itemId, int page)
    {
        this.dbService.DeletItem(expenseId);
        return PartialView("SubView", this.GetPagedList(page));
    }

1 Comment

That changed nothing at all unfortunately

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.