简体   繁体   English

在 ASP.net MVC 中,我可以在完成 controller 操作之前处理我的 cshtml 逻辑吗?

[英]In ASP.net MVC Can I process through my cshtml logic before finishing the controller action?

I have a controller who has database logic occurring in the controller action.我有一个 controller,他的数据库逻辑发生在 controller 操作中。 In addition, I know it's probably not ideal but I have some logic occurring my views.此外,我知道这可能并不理想,但我的观点有一些逻辑。 This does some sql queries and potentially those queries might update records.这会执行一些 sql 查询,并且这些查询可能会更新记录。 I have a transaction that I open at the beginning of my controller's action.我有一个在控制器操作开始时打开的事务。 In an attempt at dealing w/ this problem, I held it until after the View() call of my action with the hope that the view logic would execute before I had to commit my transaction.在尝试处理这个问题时,我将它保留到我的操作的 View() 调用之后,希望视图逻辑在我必须提交事务之前执行。 However, this doesn't seem to work as it only seems to process the view logic after the controller's action fully completes.但是,这似乎不起作用,因为它似乎只在控制器的操作完全完成后处理视图逻辑。

ViewBag.Message = message;
                ViewBag.Test = test;

                var result = View();

                LegacyDataManager.Commit();
                Data.Commit();

                return result;
            }
            finally
            {
                LegacyDataManager.Stop();
                Data.CloseInstance();
            }

An alternative solution is that I hold off performing the closing of my transactions until the end of the view's logic but this seems even more wrong than what I'm currently trying.另一种解决方案是我推迟关闭我的事务,直到视图逻辑结束,但这似乎比我目前正在尝试的更错误。

My end goal is that all necessary logic occurs before I close the transaction.我的最终目标是在我关闭交易之前发生所有必要的逻辑。

Thanks.谢谢。

In ASP.net MVC Can I process through my cshtml logic before finishing the controller action?在 ASP.net MVC 中,我可以在完成 controller 操作之前处理我的 cshtml 逻辑吗?

No.不。

The control flow is that the controller performs its logic and then passes control to the view when it's done and ready to render the view.控制流程是 controller 执行其逻辑,然后在完成并准备好呈现视图时将控制权传递给视图。

It sounds like the root of the problem is that you have too much logic in your view.听起来问题的根源在于你的观点有太多的逻辑。 For example:例如:

This does some sql queries and potentially those queries might update records.这会执行一些 sql 查询,并且这些查询可能会更新记录。

That should definitely not happen in a view.绝对不应该在视图中发生。 A view just renders the result, nothing more.视图只是呈现结果,仅此而已。 The only logic therein would be conditionally rendering the view differently based on the state of the model.其中唯一的逻辑是根据 model 的 state 有条件地渲染视图。 It should not have side-effects outside of rendering the view.应该有渲染视图之外的副作用。

The current design is fundamentally flawed, your best bet is to correct the design of your application instead of trying to coerce the framework to fit your design.当前的设计存在根本缺陷,最好的办法是纠正应用程序的设计,而不是试图强制框架适应您的设计。

暂无
暂无

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

相关问题 当我在asp.net mvc控制器操作中验证失败时,如何保留我的URL - how can i keep my url when my validation fail in asp.net mvc controller action 如何基于ASP.NET MVC中的控制器操作在页面javascript中设置变量? - How can I set a variable in my pages javascript based on my controller action in ASP.NET MVC? 在ASP.NET MVC中调用任何控制器操作之前处理cookie - Process cookies before any controller action is invoked in ASP.NET MVC 如何使用 ASP.NET MVC 中的 asp-controller 或 asp-action 访问 url 中的多个层 - How can I access multiple layers in my url using asp-controller or asp-action in ASP.NET MVC 如何在ASP.NET MVC 5.1项目中通过控制器的动作使用标准JSON格式器? - How can I use the standard JSON formatter from an action of my controller in an ASP.NET MVC 5.1 project? 如何将数据库逻辑从我的 Asp.Net MVC 应用程序转移到 ASP.Net Core MVC? - How can I transfer the database logic from my Asp.Net MVC Apllication to ASP.Net Core MVC? 什么[ASP.net] MVC在我的控制器之前做什么? - Whats [ASP.net]MVC doing BEFORE my controller? 如何从MVC控制器中的进程刷新cshtml文件? - How can I refresh my cshtml file from a process in my MVC Controller? 在 ASP.NET MVC 中执行控制器后如何将数据发送到操作过滤器? - How Can I Send Data to the Action Filter after Execution of the controller in ASP.NET MVC? 给定一个URL和HTTP动词,如何在ASP.NET MVC / Web API中解析控制器/动作? - Given a URL and HTTP verb, how can I resolve the controller/action in ASP.NET MVC/Web API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM