[英]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.