简体   繁体   English

MVC,Ajax在局部视图中的开始只能工作一次

[英]MVC, Ajax beginform in partial view only works once

I am having an odd issue with a partial view that has an ajax form in it. 我有一个奇怪的问题,部分视图中有一个ajax形式。 It works once but after that it, the controller at least believes it works fine but the partial view content does not update even though the data at least in the controller has actually has an updated model. 它工作一次但在此之后,控制器至少认为它工作正常但部分视图内容不更新,即使至少在控制器中的数据实际上具有更新的模型。

I have no clue what do to. 我不知道该做什么。

Can it have something to do with the div I an replacing the data with is outside the partial view? 它可以与div有关吗?在局部视图之外替换数据?

Should I just not use the ajax form and just use jquery to do an ajax request to the controller? 我应该不使用ajax表单并只使用jquery对控制器执行ajax请求吗?

So I have this form in my View (doesn't matter if it is in my view or the partial view) and a div that holds the html from the partial 所以我在我的视图中有这个表单(如果它在我的视图或部分视图中无关紧要)和一个div来保存部分html

 @using (Ajax.BeginForm("ChangePage", "Staging", null, new AjaxOptions { HttpMethod = "post", InsertionMode = InsertionMode.ReplaceWith, UpdateTargetId = "divResults", OnBegin = "showLoading", OnComplete = "hideLoading" }, new { id = "frmPage" }))
    {
        <input type="hidden" id="pageDir" name="pageDir" />
        <input type="hidden" id="codeFilter" name="codeFilter" />
        <input type="hidden" id="glauFilter" name="glauFilter" />
        <input type="hidden" id="gacctnoFilter" name="gacctnoFilter" />
        <input type="hidden" id="glcoFilter" name="glcoFilter" />
        <input type="hidden" id="employeeFilter" name="employeeFilter" />
        <input type="hidden" id="maxPages" name="maxPages" />
        <input type="hidden" id="page" name="page" />
    }
<div id="divResults" name="divResults">
    @Html.Partial("~/Views/Staging/_stagingResults.cshtml", Model)
</div>

I have JS that does the form submit in the partial view. 我有JS在部分视图中提交表单。 All of this works just fine. 所有这一切都很好。 I can click the button, controller gets hid, generates my new model, the partial view generates HTML and it gets displayed in divResults BUT ONLY on the first time. 我可以单击按钮,控制器获取隐藏,生成我的新模型,部分视图生成HTML,它只在第一次显示在divResults中。

Any other page change made does everything above but it won't put the new HTML in divResults. 所做的任何其他页面更改都会执行上述所有操作,但不会将新HTML放入divResults中。 In chrome's dev console I can see the new request come out and come back with the correct HTML but it is never replaced. 在chrome的开发控制台中,我可以看到新请求出来并返回正确的HTML但它永远不会被替换。

You can add a javascript function on success() method in AJAX begins form properties, which will fire after your ajax request completely loaded 您可以在AJAX开始表单属性中的success()方法上添加一个javascript函数,该函数将在您的ajax请求完全加载后触发

  @using (Ajax.BeginForm("AjaxOptionPostData", "Home", new AjaxOptions { HttpMethod = "POST", OnSuccess = "OnSuccess" }))   

Here OnSuccess will be your javascript method 这里的OnSuccess将是你的javascript方法

    function OnSuccess()
     {
       //You can do your stuff here
     }

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

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