MVC:如何以与Webforms ascx控件相同的效率将逻辑封装在MVC ascx部分视图中?

发布于 2024-09-13 23:55:30 字数 1996 浏览 5 评论 0原文

我必须在 MVC 应用程序中构建一些 ascx 部分视图来封装可重用的功能以及存档 SOR 和 SOC。然而,我遇到了如何封装驱动 MVC ascx 视图的业务逻辑的挑战。

在 Web 表单中,ASCX 控件的后台代码可以处理按钮单击事件,并且浏览器仍然显示相同的页面,只有一次回发。如何在 MVC 中归档相同的内容?当 ~/Address/Edit/2 包含一个带有调用另一个 MVC 操作的按钮的 ascx 部分视图时 - 比方说 ~/ShareController/CommonAction - CommonAction 返回到包含该视图的同一视图的最佳方式是什么ascx 文件,我需要什么?

也许我缺少 MVC Web 开发中的常见模式?

谢谢,

最大

更新:这是关于我正在寻找的模式的更多描述:

部分视图应该是地址编辑器。它可以验证地址,显示 验证错误并将信息保存回数据库。

地址编辑器将位于具有不同控制器/操作的许多页面上。

在经典的 webform 中,ascx 控件可以通过多次回发与用户完美交互,而无需更改 url 或干扰整个页面流程。 MVC 中类似场景的正确模式是什么?

更新 2:

不确定这与 Webform 的状态性有什么关系。完全不同的功能。这是关于一个控制器调用另一个子控制器(或子控制器)来处理部分视图。请参阅本评论末尾的我的示例。

我只是想达到相同级别的封装和关注点分离。如果 MVC 框架不具备这一点,那就是一个严重的弱点。

在 MVC 中,我们如何拥有一个“事物”(或部分视图,因为缺乏更好的选择)能够处理表示和逻辑并驻留在另一个父页面中而不受任何干扰?

据我了解,MVC 中的部分视图只是表示,无法在不干扰主控制器的情况下拥有专用控制器。我希望我弄错了

给你另一个例子,考虑一下你正在阅读的这个页面“stackoverflow.com/questions/{id}”。它包括一个“添加评论”组件。添加评论可能会进行服务器端验证以防止无效评论。如何创建一个可以驻留在任何页面上并处理必要的控制器逻辑的端到端“添加评论”组件?我能想到的唯一方法是修改主机页面的控制器,以便能够向用户提供验证消息并要求他们通过回发来纠正错误。这意味着你将组件添加到10个不同的页面,你必须修改10个不同的控制器。这会杀死“干”,

我希望我错了!因为到目前为止我很喜欢 MVC。

结论:

我将肯尼的回复标记为答案;然而,从封装、可重用性和 DRY 原则的角度来看,我认为这是 MVC 框架的一个严重弱点。

我的观点与网络表单的状态性无关。

请允许我解释一下:

在 webforms 中,您可以 100% 将复杂的功能封装在 ascx 控件中,当然还有相关的 classlib DLL 中。一 (1) 名开发人员可以花费数周的时间来开发它。一旦他的工作完成,没有人需要知道任何事情,只需使用它即可。用户控件可以在许多页面中重用,而不会打扰其他“开发人员用户”进行 ascx 组件内发生的详细活动。

相比之下,您永远无法使用 ASP.NET MVC 实现相同的目标,其中 ascx 部分需要与用户交互。一旦ascx需要交互,它需要它自己的控制器,并且控制器需要保存ModelState。

Jeremy Skinner 在 MVCContrib 中介绍了 [ModelStateToTempData]: http://www .jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/ 然而,这只是一个补丁。

一个好的框架应该允许开发人员 100% 封装他们的工作——在控件、部分、ascx 或其他任何形式中——并将其提供给团队的其他成员,这样他们就可以在不知道任何数据的情况下“直接使用它”。 Winform 在许多复杂的场景中完美地实现了这一点。

无论如何欢迎。

I have to build a few ascx partial views in my MVC applications to encapsulate re-usable functionalities as well as archive SOR and SOC. However, I encountered challenge how to encapsulate the business logic that drives MVC ascx views.

In webforms, the code behind of an ASCX control can handle a button click event and browser still shows the same page with only one postback. How can I archive the same thing in MVC? When ~/Address/Edit/2 includes an ascx partial view with a button on it that calls another MVC action - let’s say ~/ShareController/CommonAction – what is the best way for the CommonAction to return to the very same view that includes the ascx file?

Perhaps I am missing a common pattern in MVC web development?

Thank you,

Max

Update: Here is more description about the pattern I am looking for:

The partial view supposed to be an address editor. It can validate the address, shows
validation errors and save the information back to the database.

The address editor is going to sit on many pages with different controller/actions.

In classic webform, an ascx control can perfectly interact with the user on its own through many postbacks without changing the url dor disturbing the overal page process. What is the right pattern for the similar scenario in MVC?

Update 2:

Not sure this has anything to do with the statefullness of webform. Totally different feature. This is about a controller call another sub-controller (or child controller) to handle a partial view. See my example at the end of this comment.

I just want to achieve the same level of encapsulation and separation of concern. If MVC framework doesn’t have that, it is a serious weakness.

In MVC, how could we have a “thing” (or partial view for lack of better alternative) that is able to handles the presentation as well as the logic and reside within another parent page without any interfere?

To my understanding, Partial views in MVC are just presentation that cannot have a dedicated controller without interfere with the main controller. I hope I have got it wrong

To give you another example, consider this page that you are reading “stackoverflow.com/questions/{id}” . It includes an “Add Comment” component. The add comment might do server side validation to prevent invalid comments. How could you create an end-to-end “Add Comment” component that can reside on any page and handles necessary controller logic? The only way I can think of is to modify the controller of host page to be able to give user validation messages and ask them to correct the errors through post back. That means you add the component to 10 different pages, you have to modify 10 different controllers. That kills “DRY”

I DO HOPE THAT I AM WRONG! because I love MVC sofar.

Conclusion:

I marked Kenny’s reply as the answer; however, I would consider this as a serious weakness of MVC framework from the encapsulation, re-usability and DRY principal perspective.

My opinion has nothing to do with statefullness of webforms.

Please allow me to explain:

In webforms, You can 100% encapsulate a complex functionality in an ascx control and of course related classlib DLLs. One (1) developer can spend weeks developing it. Once his work is done, no one needs to know anything about it and just use it. The user control can be reused in many pages without bothering other “developer user”s with the detail activities that takes place within the ascx component.

In contrast you never can achieve the same with ASP.NET MVC where the ascx partial needs to interact with the user. Once the ascx needs to interact, it needs it’s own controller and the controller needs to preserve the ModelState.

Jeremy Skinner introduced the [ModelStateToTempData] in MVCContrib:
http://www.jeremyskinner.co.uk/2008/10/18/storing-modelstate-in-tempdata-with-aspnet-mvc/
However, this is just a patch.

A good framework should allow developer to 100% encapsulate their work – in a control, partial, ascx or whatever – and had it to the rest of the team so they can “just use it” without knowing any data. Winform perfectly delivers this in many complex scenarios.

Any though welcome.

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

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

发布评论

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

评论(4

记忆で 2024-09-20 23:55:30

我不知道我是否能 100% 解决你的问题,但你不能只向 UserControl 提供你需要的数据吗?

传入一个<%= Url.Action() %>单击时提交会转到该位置。如果您希望页面重定向回您所在的同一页面,您可以重定向并重新保存您的 ModelState (http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best -practices-part-1.aspx 技巧 #13)。重定向可以通过引用者或通过将操作和控制器名称发送到您的操作方法来完成。

如果这不起作用,您可以随时使用 Ajax 吗?

I dont know if I get your problem to 100% but cant you just provide the UserControl with the data you need?

Pass in a <%= Url.Action() %> that the submit goes to when clicked on. If you then want the page to redirect back to the same page you were on you canredirect and re-save your ModelState (http://weblogs.asp.net/rashid/archive/2009/04/01/asp-net-mvc-best-practices-part-1.aspx tip #13). The redirect can be done referrer or by sending in the action and controller name to your action-method.

If that doesn't work you can always use Ajax?

悲凉≈ 2024-09-20 23:55:30

我几乎可以肯定这不是“应该”完成的方式,但我通常会查看引用 URL 并重定向到那里。

I'm almost sure this is not how it's "supposed" to be done, but I usually look at the referer URL and redirect there.

滥情空心 2024-09-20 23:55:30

我认为在 MVC(以及几乎任何其他非 Web 表单)中处理此类功能的正确方法是通过 ajax 调用。

正如您所看到的,创建整页回发会很尴尬。但是,您可以通过将 post 操作和验证错误显示合并到 ajax 操作中来完成您想要的一切,该操作仅更新包含控件的页面部分,而不是整个页面。

I think the proper way to handle this type of functionality in MVC (and practically anything else that isn't webforms) is through an ajax call.

Creating a full-page postback would be awkward, as you have seen. You can, however, accomplish everything you want by incorporating your post operation and validation error display in an ajax operation that only updates the portion of the page that contains the control, and not the entire page.

眼眸里的那抹悲凉 2024-09-20 23:55:30

我正在寻找同样的东西,我遇到了 Html.Action 和 .RenderAction ,它们似乎在服务器上执行控制器并返回/注入输出。

这可以用于 MVC 中的封装和可重用性,但我还没有尝试过。

I'm looking for same thing and I came across Html.Action and .RenderAction which seem to execute controller on the server and return/inject output.

That could be used for encapsulations and reuseability in MVC, but I'm yet to try it.

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