简体   繁体   English

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

[英]RedirectToAction() vs. View() and the ternary operators?

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. 在决定从Controller Action返回哪个ActionResult时,我决定使用三元运算符而不是更长的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. 如果使用if-else,我不必进行显式转换。 Plus both RedirectToAction() and View() return an ActionResult derivative. 另外,RedirectToAction()和View()都返回一个ActionResult衍生物。

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. 虽然我确定这很明显,但EditViewModel是我的Edit动作的视图模型,updatedCategory是EF4对象。 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. 好的...我刚刚意识到我在做什么是不必要的,因为无论我是回到使用updatedCategory的Edit操作,所以我不需要确保模型有效。 I am still curious to know the answer to the question if anyone can help. 如果有人能提供帮助,我仍然很想知道这个问题的答案。

I believe it's because the arguments when using the ?: operator have to be convertable between themselves, eg in condition ? 我相信这是因为使用?:运算符时的参数必须在它们之间转换,例如在条件中? x : y you need to be able to convert x to y or y to x. x:y您需要能够将x转换为y或y转换为x。 Then the type of the result is the least specific of the two. 然后结果的类型是两者中最不具体的。 So if x was an object and ya string then you can cast a string to an object and the result would be of type object. 因此,如果x是一个对象而你是一个字符串,那么你可以将一个字符串转换为一个对象,结果将是object类型。

In your example x is a RedirectToRouteResult and y is a ViewResult. 在您的示例中,x是RedirectToRouteResult,y是ViewResult。 You cannot convert a RedirectToRouteResult to a ViewResult or vice versa. 您无法将RedirectToRouteResult转换为ViewResult,反之亦然。 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. 可以将它们都转换为ActionResult,这就是为什么如果你转换为ActionResult它可以工作 - 然后x的类型是ActionResult,y可以转换为ActionResult,整体结果是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... 希望我已经在那里正确地解释了...害怕我不知道?:运算符的确切语义,因为我自己很少使用它...

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;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM