寻找有关不带回发的页面更新的建议
所以我正在做一个个人MVC项目来练习。我喜欢使用 ajax 调用 Web 服务来更新页面的响应能力,而不必执行整个回发。
我的知识非常有限,我只知道能够使用 Javascript 来做到这一点。
然而,从新开发者的角度来看,这存在一个问题。假设您需要在大部分站点上对域对象执行任何 CRUD 操作,执行相同的行为。因此导致(对于新开发人员来说)缺乏对 Javascript 的掌握(调试、单元测试、强类型等)。
但是由于我缺乏知识,有没有一种方法可以在 MVC 中获得相同的行为,而不必通过 javascript/ajax 调用 Web 服务来执行所有 CRUD 操作?
注意:在因为我对 Javascript 的松散言论而责备我之前......我喜欢 Javascript,并且可能有一些方法可以否定我在所有 CRUD 操作中使用它所提出的一些观点我可能没有意识到我寻求的行为。
So I am working on a personal MVC project for practice. I like the responsiveness with using ajax calls to web services to update the page and not have to do an entire post back.
My knowledge is very limited and I am only aware of being able to do this with Javascript.
However, from a new developer's perspective, there is one problem with this. It would be assumed that you would need to carry out this same behavior through most of your site for any of your CRUD operations on your domain objects. Therefore causing (again for a new developer) the lack of handholding with Javascript (debugging, unit testing, strong type...etc).
But do to my lack of knowledge, is there a way you can acquire this same behavoir in MVC without having to perform all your crud operations with javascript/ajax calls to web services?
NOTE: before beating me up about my loose remarks of Javascript....I like Javascript, and there might be ways to negate some of the points I made with using it for all your CRUD operations to get the behavior I seek that I might just not be aware of.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的问题,听起来您正在寻找 MVC 的 UpdatePanel。此控件对于 ASP.NET MVC 无效,但您可以通过使用 AJAX(Microsoft.Ajax 库或类似 jQuery 的库)并从控制器返回 PartialView 来完成类似的功能。有一个不错的教程 关于 Telerik 网站上的这项技术(只需忽略他们渲染自己的控件的部分 - 您几乎可以渲染任何内容)。
如果这不能满足您的需求,我相信,为了在您的一个或多个页面上完成类似 AJAX 的行为,必须涉及一定程度的 JavaScript。
您可能需要考虑使用 JavaScript 框架,例如 jQuery 或 MooTools 来包装 AJAX 调用行为,这样您只需调用已经测试过的 AJAX 调用(例如
jQuery.ajax()
)。那么,您的 JavaScript 只是更新页面的回调处理程序。在服务器端,由于您使用的是 MVC(我假设是 ASP.NET MVC),因此您的控制器可以只返回
JsonResult
而不是ActionResult
并且只需在控制器中进行 CRUD 操作,而不是创建单独的 Web/WCF 服务套件。 (您也可以将ActionResult
返回到您的jQuery.ajax()
调用。)所以我认为您无法摆脱JavaScript,以便在您的网站中完成 AJAX 风格的行为。您可能不必创建一套 Web 服务,因为您只需将它们包含在控制器中即可。
这可能会让你得到你想要的。您可能想查看
Based on your question, it sounds like you're looking for an UpdatePanel for MVC. This control isn't valid for ASP.NET MVC, but you could accomplish similar functionality by using AJAX (either the Microsoft.Ajax lib or something like jQuery) and returning a PartialView from your controller. There's a decent tutorial on this technique on Telerik's site (just ignore the parts where they render their own controls -- you could render pretty much anything).
If this doesn't met your needs, the I believe, in order to accomplish an AJAX-like behavior on your page or pages, there will have to be some degree of JavaScript involved.
You may want to consider using a JavaScript framework like jQuery or MooTools in order to wrap the AJAX calling behavior, so that you just have to invoke the already-tested AJAX calls (e.g.
jQuery.ajax()
). Your JavaScript, then, would just be the callback handlers to update your pages.On the server side, since you're using MVC (which I'm assuming is ASP.NET MVC), your controllers could just return a
JsonResult
instead of aActionResult
and just have your CRUD operations inside your controller instead of create a separate suite of web/WCF services. (You could also return anActionResult
to yourjQuery.ajax()
call, too.)So I don't think you're going to be able to get away from JavaScript in order to accomplish AJAX-style behavior in your web site. You may not have to create a suite of web services as you could just have them contained within your controller.
This is probably going to get you what you want. You may want to check out a tutorial on the ASP.NET web site for some additional insight (but that won't get you to the unit testing part of your question).