RedirectToAction() 与 View() 和三元运算符?

发布于 2024-10-14 18:42:37 字数 750 浏览 2 评论 0原文

在决定从控制器操作返回哪个 ActionResult 时,我决定使用三元运算符,而不是更长的 if-else。这是我的问题...

这段代码可以工作

return
    ModelState.IsValid ?
    (ActionResult) RedirectToAction("Edit", new { id = id }) :
    View(new EditViewModel(updatedCategory));

,但

return 
     ModelState.IsValid ?
     RedirectToAction("Edit", new { id = id }) :
     View(new EditViewModel(updatedCategory));

如果使用 if-else,我就不必进行显式转换。另外,RedirectToAction() 和 View() 都返回一个 ActionResult 衍生物。

我喜欢这段代码的简洁性,但转换似乎不正确。谁能启发我吗?

虽然我确信这是显而易见的,但 EditViewModel 是我的编辑操作的视图模型,updatedCategory 是 EF4 对象。但我认为这与问题无关。

好吧...我刚刚意识到我所做的事情是不必要的,因为无论如何我都会返回到带有updatedCategory 的编辑操作,所以我不需要以确保模型有效。我仍然很想知道这个问题的答案是否有人可以提供帮助。

when deciding on which ActionResult to return from a Controller Action I decided to use the ternary operators as opposed to the lengthier if-else. Here is my issue...

this code works

return
    ModelState.IsValid ?
    (ActionResult) RedirectToAction("Edit", new { id = id }) :
    View(new EditViewModel(updatedCategory));

but this doesn't

return 
     ModelState.IsValid ?
     RedirectToAction("Edit", new { id = id }) :
     View(new EditViewModel(updatedCategory));

I would not have to do the explicit casting if using an if-else. Plus both RedirectToAction() and View() return an ActionResult derivative.

I like the terseness of this code but that casting doesn't seem right. Can anyone enlighten me?

Though I'm sure this is obvious, the EditViewModel is a view model for my Edit action and updatedCategory is an EF4 object. But I don't think this is relevant to the issue.

ok... I just realized what I was doing is unnecessary because regardless I am going back to the Edit action with the updatedCategory, so I don't need to make sure the Model is valid. I am still curious to know the answer to the question if anyone can help.

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

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

发布评论

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

评论(2

绝對不後悔。 2024-10-21 18:42:37

我相信这是因为使用 ?: 运算符时的参数必须可以在它们之间转换,例如在条件 ? x : y 您需要能够将 x 转换为 y 或 y 转换为 x。那么结果的类型是两者中最不具体的。因此,如果 x 是一个对象并且 ya 是字符串,那么您可以将字符串转换为对象,结果将是对象类型。

在您的示例中,x 是 RedirectToRouteResult,y 是 ViewResult。您无法将 RedirectToRouteResult 转换为 ViewResult,反之亦然。然而,您可以将它们都转换为 ActionResult,这就是为什么如果您转换为 ActionResult 它会起作用 - x 的类型是 ActionResult,y 可以转换为 ActionResult ,总体结果是类型为 ActionResult。

希望我在那里正确地解释了自己...担心我不知道 ?: 运算符的确切语义,因为我自己很少使用它...

I believe it's because the arguments when using the ?: operator have to be convertable between themselves, e.g. in condition ? x : y you need to be able to convert x to y or y to x. Then the type of the result is the least specific of the two. So if x was an object and y a string then you can cast a string to an object and the result would be of type object.

In your example x is a RedirectToRouteResult and y is a ViewResult. You cannot convert a RedirectToRouteResult to a ViewResult or vice versa. You can convert them both to an ActionResult however, which is why if you cast to an ActionResult it works - the type of x is then an ActionResult,y can be converted to an ActionResult and the overall result is of type ActionResult.

Hope I've explained myself correctly there... Afraid I don't know the exact semantics of the ?: operator as I rarely use it myself...

著墨染雨君画夕 2024-10-21 18:42:37

赋值变量上的数据类型必须完全相同,并且这里的两种返回类型是我能想到的最简单的示例:

int originalValue = 10;
int? value = (originalValue != 10) ? null : originalValue;

//Which is very easily fixed with type casting as you have done

int? value = (originalValue != 10) ? null : (int?)originalValue;

The data types have to be exactly the same on the assignment variable and both return types here is the most simple example I can think of:

int originalValue = 10;
int? value = (originalValue != 10) ? null : originalValue;

//Which is very easily fixed with type casting as you have done

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