简体   繁体   English

使用MVC Ajax在部分视图中更新部分视图

[英]Update Partial View Within Partial View Using MVC Ajax

I have an MVC 5 web application that contains a Razor View called CreateProposal and it accepts a ViewModel called ProposalViewModel. 我有一个MVC 5 Web应用程序,它包含一个名为CreateProposal的Razor View,它接受一个名为ProposalViewModel的ViewModel。 This View, includes a reference to a Partial View called _Proposal which also accepts the ViewModel. 此视图包含对名为_Proposal的部分视图的引用,该部分视图也接受ViewModel。

CreateProposal View CreateProposal视图

@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还引用了另一个名为_ExistingAttachments的部分视图,它也接受ViewModel。

_Proposal Partial _Proposal Partial

@model STAR.UI.ViewModels.ProposalViewModel

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

_ExistingAttachments Partial _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. _ExistingAttachments Partial吐出一个href链接列表和每个旁边的Remove链接。 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. 一旦用户单击要删除的项目上的删除链接,该条目的ID就会使用一些JQuery存储在隐藏文本框中。

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调用,然后应该更新Partial _Proposal中的Partial _ExistingAttachments。

$.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 MVC控制器

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. 一切正常,直到我期望刷新Partial View _ExistingAttachments ,但这不会发生。

Apologies for the long question, but hopefully can spot what I am doing wrong. 为长期问题道歉,但希望能发现我做错了什么。

Please help. 请帮忙。

UPDATE UPDATE

I should add, the code makes it to Ajax Success function and alert("Worked."); 我应该补充一下,代码使它成为Ajax Success函数和alert(“Worked。”); is called. 叫做。 However, instead of the Partial Refresh, my Edit Action within the same Controller is called 但是,不是部分刷新,而是调用同一个Controller中的编辑操作

[HttpPost]
public ActionResult EditProposal(ProposalViewModel model)

Folks, finally got it solved thanks to Jasen's help. 感谢Jasen的帮助,人们终于解决了问题。 After my Ajax call was complete, the code then redirected to another page. 在我的Ajax调用完成后,代码然后重定向到另一个页面。 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. 这导致应用程序在Ajax调用后执行POST。 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. 现在一切都按预期工作了。

Seems all your markup and code blocks good. 似乎所有标记和代码块都很好。 Probably your ajax get request cached 可能是你的ajax get请求被缓存了

Try by adding cache:false to your ajax call 尝试将cache:false添加到您的ajax调用中

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

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM