Can I solve this? (?:)

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following:

return returnUrl == null ? RedirectToAction("Index", "Home") : Redirect
(returnUrl);

I get an error:

Type of conditional expression cannot be determined because there is
no implicit conversion between 'System.Web.Mvc.RedirectToRouteResult'
and 'System.Web.Mvc.RedirectResult'

Is there a way to fix this without needing to use an IF?

Thanks,

Miguel
 
I have the following:

return returnUrl == null ? RedirectToAction("Index", "Home") :
Redirect (returnUrl);

I get an error:

Type of conditional expression cannot be determined because there is
no implicit conversion between 'System.Web.Mvc.RedirectToRouteResult'
and 'System.Web.Mvc.RedirectResult'

Is there a way to fix this without needing to use an IF?

I'd use an 'if' statement here, for readability---if statements are
easier to quickly parse with your head when compared to (most) usages
of the ternary operator. IMHO, the ternary operator should only be
used with either a variable or a literal for the possible return
values, and function calls should be done in if statements.

It looks as if you're in need of parenthesis. Try:

return((returnUrl == null) ? RedirectToAction("Index", "Home") :
Redirect(returnUrl));

If you're still getting an error, then you probably need casts to
something that makes sense within the context of your code.

--- Mike
 
I'd use an 'if' statement here, for readability---if statements are
easier to quickly parse with your head when compared to (most) usages
of the ternary operator.  IMHO, the ternary operator should only be
used with either a variable or a literal for the possible return
values, and function calls should be done in if statements.

It looks as if you're in need of parenthesis.  Try:

return((returnUrl == null) ? RedirectToAction("Index", "Home") :
  Redirect(returnUrl));

If you're still getting an error, then you probably need casts to
something that makes sense within the context of your code.

        --- Mike

Thanks!
 
shapper said:
Hello,

I have the following:

return returnUrl == null ? RedirectToAction("Index", "Home") : Redirect
(returnUrl);

I get an error:

Type of conditional expression cannot be determined because there is
no implicit conversion between 'System.Web.Mvc.RedirectToRouteResult'
and 'System.Web.Mvc.RedirectResult'

Is there a way to fix this without needing to use an IF?

Thanks,

Miguel

Just cast one or both of the result operands to the data type of the
return value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top