简体   繁体   English

使用MVC3中的viewmodel中的新数据更新视图,C#

[英]Updating the view with the new data in the viewmodel in MVC3, C#

I have ajax call of action in the controller, after it update the database and it is completed successfully I can do like this: 我在控制器中有ajax动作调用,在更新数据库并成功完成后我可以这样做:

return PartialView("Overview", mydatamodel);

and then in the the success to do like this: 然后在成功中这样做:

success: function (data) { 
            // do something with the data => refresh some
            // portion of your DOM
            $('#someDivId').html(data);
        }

And it would work fine, but what I need is that a collection in the view model to be updated, and the whole view to be rendered again with the new data. 并且它可以正常工作,但我需要的是视图模型中的集合要更新,并且整个视图将使用新数据再次呈现。

I can do that if for instance I have submit button, then whole view is updated with the new data but if I have ajax call, how can i do that. 我可以这样做,例如我有提交按钮,然后整个视图用新数据更新,但如果我有ajax调用,我该怎么做。

Here is link to my previous post where have more details: MVC3 receiving the new model data after submit 这里是我上一篇文章的链接,其中有更多细节: MVC3在提交后接收新的模型数据

Thank you in advance! 先感谢您!

If you want to update the entire view don't use AJAX. 如果要更新整个视图,请不要使用AJAX。 Simply use a submit button. 只需使用提交按钮即可。 The whole point of AJAX is to update only a portion of the view without navigating away from the current page. AJAX的重点是只更新视图的一部分而不导航离开当前页面。

By the way you could redirect on the client side using window.location.href : 顺便说一句,您可以使用window.location.href在客户端重定向:

success: function (data) { 
    window.location.href = '@Url.Action("Overview", "SomeController")';
}

but there's really no need to do that if you will always redirect in the success AJAX callback. 但是如果你总是在成功的AJAX回调中重定向,那么真的没有必要这样做。 You should not use AJAX in this scenario. 在这种情况下,您不应该使用AJAX。

You usually use AJAX precisely for the situations where you only want a portion of the view to be updated after the request completion. 您通常会精确地使用AJAX,以便在请求完成后只需要更新视图的一部分。

If you, for some reason, need to use AJAX even for such cases (eg using a DELETE HTTP verb to send the request), you might do something like 如果您出于某种原因,即使在这种情况下也需要使用AJAX(例如使用DELETE HTTP动词发送请求),您可能会做类似的事情

window.location.href = '/Items/123';

in your success callback function, which effectively triggers a full page update. 在您的success回调函数中,它有效地触发整页更新。

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

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