简体   繁体   English

MVC PartialView循环返回上一个PartialView

[英]MVC PartialView Cycles Back to Previous PartialView

I have a MVC Controller with an Index that will cycle through multiple PartialView as the user goes through the form process. 我有一个带有索引的MVC控制器,当用户完成表单处理时,它将循环遍历多个PartialView

Here is the Index: 这是索引:

@section pageMain {
    <div id="partialView">
        @Html.Partial("SelectAccount", SUPR.Models.Account.GetAccounts());
    </div>
}

This works fine and I get the SelectAccount page just fine. 这工作正常,我得到SelectAccount页面就好了。

My issue is that I have an AJAX call that runs when the user selects a row in a table on the SelectAccount page. 我的问题是,当用户在SelectAccount页面上的表中选择一行时,我会运行一个AJAX调用。 What is supposed to happen is that the PartialView is then replaced with another PartialView that displays the details of the selected row. 应该发生的是PartialView然后被另一个显示所选行的详细信息的PartialView替换。 This actually does work, but after about 1 second it cycles back to the SelectAccount PartialView with a query string attached, for some reason, to the URL. 这实际上确实有效,但是大约1秒后它会循环回到SelectAccount PartialView ,并且由于某种原因,它附加了一个查询字符串到URL。

Before 之前

After

Here is the code for the AJAX call: 这是AJAX调用的代码:

$.ajax({
    type: "GET",
    url: "/Review/GetReviewView",
    data: { profitCenter: profitCenterNo }
}).done(function (data) {
    $('#partialView').html(data);
});

The ONLY that is happening in the controller for GetReviewView is that I'm returning the PartialView with the PartialView name and model. GetReviewView的控制器中发生的唯一情况是,我正在使用PartialView名称和模型返回PartialView This working because, in that brief moment that I can see the ReviewAccount view, the proper data is in the fields. 这样做是因为,在我可以看到ReviewAccount视图的那个短暂时刻,正确的数据在字段中。

Any thoughts as to why this odd behavior is occurring? 有关为什么会发生这种奇怪行为的任何想法?

Are you capturing the event in JS? 你在JS中捕获event了吗? If you are triggering off of a click event then add the event parameter to the method and then at the end of the method add: 如果触发click事件,则将event参数添加到方法中,然后在方法结束时添加:

event.preventDefault()

And see if it still happens. 并看看它是否仍然发生。 It sounds like a client side JS issue, not a C# issue. 这听起来像是客户端JS问题,而不是C#问题。

The way you are doing this is actually fine. 你这样做的方式实际上很好。 There used to be a Microsoft library called ASP-AJAX or something, don't remember because I didn't use it much. 曾经有一个名为ASP-AJAX的微软库或其他东西,不记得了,因为我没有太多使用它。 This library basically allowed you to decorate HTML elements with ASP-AJAX-METHOD , ASP-AJAX-REFRESH-TYPE and it would automatically do something similar to what you are doing now. 这个库基本上允许你用ASP-AJAX-METHOD,ASP-AJAX-REFRESH-TYPE来装饰HTML元素,它会自动执行类似于你现在正在做的事情。 Go to the Server and retrieve a PartialView. 转到服务器并检索PartialView。

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

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