是否可以创建一个非 ActionResult 方法来返回 ActionResult...或者最好/最巧妙的解决方法?
我有一个来自数据库的对象,该对象在我的应用程序的很多地方使用。
实际的精确对象构建起来有点复杂,特别是在开发过程中,我已经对其进行了多次更改。因此,我从控制器中提取了该方法,并构建了一个具有对象返回类型的方法。
但是,该对象可能不存在,如果不存在,我的代码将创建它并返回它。
例如:
public ActionResult Index()
{
var model = GetTheObject();
return View(model);
}
public MyComplicatedObject GetTheObject()
{
MyComplicatedObject passback = ...database query....
if(passback==null)
create the object here....
return passback;
}
但是,我不再想创建默认对象。如果它不存在,我希望将用户发送到一个视图以创建一个新视图。
我不知道是因为我在凌晨 4 点左右才开始编码,还是因为我水平不够好,但是我却没有找到一种巧妙的方法。
我知道以下内容行不通,但理想情况下这就是我想要的:
public MyComplicatedObject GetTheObject()
{
MyComplicatedObject passback = ...database query....
if(passback==null)
return View("CreateObject");
return passback;
}
但显然,这行不通。
我能想到的最好的解决方案是基本上返回 null 或异常,然后使用 if(passback==null)
&return View("CreateObject");
(如果为 null)在 ActionResult 上。
然而,由于我想在一些地方重复这一点,因此更有意义的是能够在 ActionResult 的一行/调用中包含 GetTheObject()
而没有其他内容。
有什么办法可以实现这一点吗?
I have an object from a database that is used in a lot of places in my application.
The actual precise object is a bit complicated to build, and, especially during development, I have changed it several times. For this reason, I extracted the method out of the Controller and built a method that has a return type of the object.
However, it was possible that this object did not exist, and if it did not, my code would create it and return it.
For example:
public ActionResult Index()
{
var model = GetTheObject();
return View(model);
}
public MyComplicatedObject GetTheObject()
{
MyComplicatedObject passback = ...database query....
if(passback==null)
create the object here....
return passback;
}
However, I no longer want to create a default object. If it does not exist, I want the user to be sent to a view to create a new one.
I don't know if it is because I am coding at almost 4AM, or if I am just not that good, but, a neat way of doing this is escaping me.
I know the following won't work, but ideally this is what I want:
public MyComplicatedObject GetTheObject()
{
MyComplicatedObject passback = ...database query....
if(passback==null)
return View("CreateObject");
return passback;
}
Obviously though, this will not work.
The best solution I can think of is to basically return either null or an exception, then have if(passback==null)
&return View("CreateObject");
(in case of a null) on the ActionResult.
However, as I want to repeat this in a few places, it makes more sense to be able to just have GetTheObject()
in one line/call from the ActionResult and nothing else.
Is there any way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个类似的场景,我想返回“NotFound”视图,以防我的存储库返回空对象。我实现了一个 ViewForModel 辅助方法以避免重复:
I have a similar scenario where I want to return a "NotFound" view in case that my repository returns a null object. I implemented a
ViewForModel
helper method to avoid repeating myself:只需从您的方法返回 null,并让您的操作方法在获得 null 时返回创建视图。
Just return null from your method, and have your action method return the create view when it gets a null.