Update Partial View Within Partial View Using MVC Ajax

tcode

I have an MVC 5 web application that contains a Razor View called CreateProposal and it accepts a ViewModel called ProposalViewModel. This View, includes a reference to a Partial View called _Proposal which also accepts the ViewModel.

CreateProposal View

@model STAR.UI.ViewModels.ProposalViewModel

<div class="row">
    @Html.Partial("_Proposal", Model)
</div>

The Partial View _Proposal also references another Partial View called _ExistingAttachments which also accepts the ViewModel.

_Proposal Partial

@model STAR.UI.ViewModels.ProposalViewModel

<div class="col-md-6" id="proposalAttachments">
     @Html.Partial("_ExistingAttachments", Model)
</div>

_ExistingAttachments Partial

@model STAR.UI.ViewModels.ProposalViewModel

<div class="form-group">
    <label>Existing Attachments</label><br />
    @Html.Hidden("hiddenAttachmentID", "", new { @id = "hiddenAttachmentID" })
    @if (Model.Attachments != null)
    {
        foreach (var upload in Model.Attachments)
        {
            <a href="~/Content/uploads/@upload.fileName">@upload.fileName</a>
            <a href="#" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#ModalDeleteAttachment" data-id="@upload.fileID">Remove</a><br />
        }
    }
</div>

_ExistingAttachments Partial spits out a list of a href links and a Remove link beside each. Once the user clicks the remove link on the item they want to remove, the id of that entry is then stored in the hidden text box using a bit of JQuery.

JQuery

$(document).on('click', '.btn.btn-danger', function () {
        var id = $(this).data('id');
        //alert(id);
        $('#hiddenAttachmentID').val(id);

    });

A modal confirm box then is presented to the user and once they confirm the remove, an Ajax call is made which is supposed to then update the Partial _ExistingAttachments within the Partial _Proposal

$.ajax({
        type: "GET",
        url: '/Proposal/DeleteAttachment/',
        data: { id: $("#hiddenAttachmentID").val() },
        error: function () {
            alert("An error occurred.");
        },
        success: function (data) {
            alert("Worked.");
            $("#proposalAttachments").html(data);
        }

});

MVC Controller

public ActionResult DeleteAttachment(int id)
{
    //Delete entry using passed in id
    ProposalViewModel model = new ProposalViewModel();
    //Code to populate ViewModel
    return PartialView("_ExistingAttachments", model);
}

Everything works well until I expect the Partial View _ExistingAttachments to be refreshed, but this doesn't happen.

Apologies for the long question, but hopefully can spot what I am doing wrong.

Please help.

UPDATE

I should add, the code makes it to Ajax Success function and alert("Worked."); is called. However, instead of the Partial Refresh, my Edit Action within the same Controller is called

[HttpPost]
public ActionResult EditProposal(ProposalViewModel model)
tcode

Folks, finally got it solved thanks to Jasen's help. After my Ajax call was complete, the code then redirected to another page. Obviously I didn't want this to happen as I just wanted the partial view to update on my page, but then remain on the page.

The culprit was actually the confirm button in my Modal. It was

<input type="submit" id="mySubmitDeleteAttachment" class="btn btn-primary" value="Yes, please delete" />

This caused the application to carry out a POST after the Ajax call. So I instead changed to this

<button type="button" id="mySubmitDeleteAttachment" class="btn btn-default">Yes, please delete</button>

And everything is now working as expected.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update Partial View Within Partial View Using MVC Ajax

From Dev

Not able to refresh the partial view after a form submission from another partial view within it, using ajax in mvc razor

From Dev

unable to update partial view using jquery ajax in mvc 4

From Dev

Partial View within Partial View in MVC: Good or bad practice?

From Dev

Asp.Net MVC Partial View Update with Ajax

From Dev

Display partial view in a view using mvc 4

From Dev

Partial View Update using Ajax refreshing the whole page

From Dev

How to update parent table from partial view using Ajax

From Dev

How to update parent table from partial view using Ajax

From Dev

MVC Ajax.BeginForm on Partial View ejecute function on Parent View update Parent View

From Dev

Cannot render partial view within partial view

From Dev

MVC Ajax with Dynamic Partial View Creation

From Dev

MVC Ajax returning Partial view but it is not showing

From Dev

AJAX request for a loaded partial view -MVC

From Dev

Returning a partial view with Ajax, MVC 3

From Dev

JQuery Ajax MVC Partial View Not working

From Dev

update partial view ajax with no action result

From Dev

MVC Partial View and Models

From Dev

MVC Partial View not rendering

From Dev

MVC Partial View Javascript

From Dev

AJAX pagedlist with partial view

From Dev

MVC 4 Using Paged List in a partial View

From Dev

Zend 2: Update a partial view using Jquery?

From Dev

Using AJAX to load a partial view not working

From Dev

How to update view model in a partial view in MVC5?

From Dev

Update main view after ajax rendered a partial view

From Dev

MVC Post Form using Ajax (from partial view) but without any redirect (remain on same main view page)

From Dev

Loading Partial View within Tooltip

From Dev

MVC3 Partial View : Not able to see html in View Source for Partial View render using Json object

Related Related

  1. 1

    Update Partial View Within Partial View Using MVC Ajax

  2. 2

    Not able to refresh the partial view after a form submission from another partial view within it, using ajax in mvc razor

  3. 3

    unable to update partial view using jquery ajax in mvc 4

  4. 4

    Partial View within Partial View in MVC: Good or bad practice?

  5. 5

    Asp.Net MVC Partial View Update with Ajax

  6. 6

    Display partial view in a view using mvc 4

  7. 7

    Partial View Update using Ajax refreshing the whole page

  8. 8

    How to update parent table from partial view using Ajax

  9. 9

    How to update parent table from partial view using Ajax

  10. 10

    MVC Ajax.BeginForm on Partial View ejecute function on Parent View update Parent View

  11. 11

    Cannot render partial view within partial view

  12. 12

    MVC Ajax with Dynamic Partial View Creation

  13. 13

    MVC Ajax returning Partial view but it is not showing

  14. 14

    AJAX request for a loaded partial view -MVC

  15. 15

    Returning a partial view with Ajax, MVC 3

  16. 16

    JQuery Ajax MVC Partial View Not working

  17. 17

    update partial view ajax with no action result

  18. 18

    MVC Partial View and Models

  19. 19

    MVC Partial View not rendering

  20. 20

    MVC Partial View Javascript

  21. 21

    AJAX pagedlist with partial view

  22. 22

    MVC 4 Using Paged List in a partial View

  23. 23

    Zend 2: Update a partial view using Jquery?

  24. 24

    Using AJAX to load a partial view not working

  25. 25

    How to update view model in a partial view in MVC5?

  26. 26

    Update main view after ajax rendered a partial view

  27. 27

    MVC Post Form using Ajax (from partial view) but without any redirect (remain on same main view page)

  28. 28

    Loading Partial View within Tooltip

  29. 29

    MVC3 Partial View : Not able to see html in View Source for Partial View render using Json object

HotTag

Archive