asp mvc适当的ajax用法

发布于 2024-11-16 06:53:27 字数 578 浏览 0 评论 0原文

我意识到我过去的许多项目都遭受了“太多ajax”的困扰。我几乎编写了所有 Intranet“业务”类型的应用程序,因此对旧版浏览器/禁用的 javascript/等的可访问性对我来说从来都不是真正的问题。但是诸如无法添加书签/返回/随机错误/超时等用户看不到的事情(我知道有办法解决这个问题,但只是作为一个例子)......而且最重要的是开发时间要长得多(对我来说这是一个大问题,我是唯一的开发者),但似乎并不是很多。

根据人们的经验 - 什么时候适合在商业应用程序/主要是 CRUD 类型的网站中使用 ajax?

以此为例:我有一个显示所有注册用户的网格。其中一个按钮会将您带到编辑/详细信息视图,您可以在其中编辑所有信息并查看网格上未显示的更多信息。您可以单击“保存”,发出 ajax 请求并返回成功消息或模型状态,其中错误转换为 JSON,并显示在“保存”按钮上方的消息中(带有一堆解析结果的 jquery)很好,无需刷新页面。我有很多很多类似的屏幕。单击“保存”并成功(顶部有某种“闪存消息”表明保存成功)跳过所有ajax/json/等,重定向回显示所有用户的网格是否更有意义?作为开发者。我很难想象什么对最终用户最有意义,但仅仅进行重定向就会简单得多,对我来说也有意义。人们在这些场景中的经历是什么?

I realized that many of my past projects have suffered from "too much ajax". I write pretty much all intranet "business" type apps so accessibility to older browser/disabled javascript/etc has never really been much of an issue for me. But things like unable to bookmark/go back/random errors/timeouts that the user doesn't see (I know there are ways around this but just as an example)...and most of all the development time is a lot longer (big issue for me I'm the sole dev) for what seems to be not a whole lot.

From people's experience - when is it appropriate to use ajax in business app/mainly CRUD type sites?

Take this as an example: I have a grid displaying all the registered users. One of the buttons takes you to an edit/details view where you can edit all the info and view further info not displayed on the grid. You can click "save" an ajax request is made and return either a success message or a modelstate with errors converted to JSON and that is displayed in a message above the "save" button (with a bunch of jquery that parses the result) all nice without a page refresh. I have many many screens similar to this. Would it make more sense just to redirect back to the grid displaying all the users once "save" is clicked and successful (with some sort of "flash message" on top stating save was successful) skipping all the ajax/json/etc? As the dev. I have a hard time imaging what would make most sense to the end users, but just doing a redirect would be much simpler and makes sense to me. What are people's experiences in these sort of scenarios?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

热风软妹 2024-11-23 06:53:27

在这种情况下,我会完全避免使用 AJAX,除非您有特定的原因。部分页面更新、保留用户在页面上的位置或其他原因。

MVC 可以很好地处理您描述的场景,包括成功/失败消息。在这种“编辑”失败案例中,您将检测服务器端的失败,并将相同的编辑视图连同模型状态信息返回给用户。它们的所有字段都将保持填充状态,您将能够轻松显示验证消息。在成功方面,您将返回列表或索引视图,并可能使用 TempData 向用户显示一条消息,指示其编辑成功。使用一点 JavaScript 来增加趣味性,并在 10 秒左右后删除消息,用户体验非常好。

当您添加客户端验证时,这会变得更好,这在 MVC3 中要容易得多。

我认为在这种情况下你可以为用户创建非常直观的体验,并完全避免 ajax/json 。我两者都做,但在开始编写客户端 ajax 代码之前,我确保我有一个合法的理由。

查看 Steve Sanderson MVC 书籍。他完全按照您详细描述的方式演练了这个场景。他的MVC3书还没有出版,我还没有读过MVC2书,但原来的MVC书有。

http://www.amazon.com/ASP-NET-Framework-Experts-Voice-NET/dp/1430228865/ref=sr_1_4?ie=UTF8&qid=1308745293&sr=8-4

当我寻找这样的基本框架指导时,我也喜欢 PluralSight 在线培训。 http://www.pluralsight-training.net/microsoft/

当然有很多免费博客以及其他内容也涵盖了 MVC。玩得开心。

In that scenario I would avoid AJAX all together unless you have a specific reason for it. Partial page updates, keeping a user's place on the page, or other reason.

MVC handles the scenario you describe very well, including the success/failure messages. In this "edit" failure case you'll detect the failure server side and return the same edit view back to the user with model state information. All of their fields will remain populated and you'll easily be able to display validation messages. On the success side you will return your list or index view and possibly use TempData to display a message to the user indicating their successful edit. A little javascript to spice that up and remove the message after 10 seconds or so and the user experience is very good.

This gets even better when you add client side validation, which is much easier in MVC3.

I think you can create a very intuitive experience for the user in this case and avoid ajax/json all together. I do both but I make sure I have a legitimate reason before I start writing client side ajax code.

Take a look at the Steve Sanderson MVC books. He walks through this scenario exactly as you described with good detail. His MVC3 book isn't out yet, and I haven't read the MVC2 book but the original MVC book has it.

http://www.amazon.com/ASP-NET-Framework-Experts-Voice-NET/dp/1430228865/ref=sr_1_4?ie=UTF8&qid=1308745293&sr=8-4

I also like PluralSight online training when I'm looking for fundamental framework guidance like this. http://www.pluralsight-training.net/microsoft/

Of course there are plenty of free blogs and what not that cover MVC too. Have fun.

仅一夜美梦 2024-11-23 06:53:27

在我的应用程序中,我必须从用户那里获取大量数据,这些数据主要来自主要业务实体(员工、分销商等),我通常采用普通表单发布的方式,而不是 ajax 表单。大多数情况下,当要发布的数据很小并且保存后必须在同一页面上显示数据时,我使用ajax。

例如,在员工视图页面上,您可以有一个小表单来为员工添加体验,您可以通过 ajax 保存它并将输入的数据附加回同一页面。

以 Stack Overflow 为例。他们使用普通形式的帖子来保存答案和问题,但当涉及到数据量较小且必须将评论附加到同一页面上的评论时,在这种情况下使用 ajax 是有意义的。

In my application where I have to take a lot of data from the user that happens mostly form main business entities (Employeee, Distributor, etc.) I usually take the route of normal form post rather than ajaxifying the form. Mostly, I use ajax when data to be posted is small and I have to display data on same page after saving.

For instance, on Employees view page you can have a little form to add experiences for the employee and you can save it through ajax and append the entered data back to the same page.

Take the example of Stack Overflow for instance. They use normal form post for saving answers and questions but when it comes to comments where amount of data is small and comments have to be appended on the same page then it makes sense to use ajax in such scenarios.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文