when deciding which ActionResult to return from the controller action, I decided to use ternary operators as opposed to the longer if-else. Here is my problem ...
this code works
return ModelState.IsValid ? (ActionResult) RedirectToAction("Edit", new { id = id }) : View(new EditViewModel(updatedCategory));
But it is not
return ModelState.IsValid ? RedirectToAction("Edit", new { id = id }) : View(new EditViewModel(updatedCategory));
I did not need to do explicit casting when using if-else. In addition, both RedirectToAction () and View () return a derived ActionResult.
I like the compression of this code, but the casting seems wrong. Can anyone enlighten me?
Although I'm sure this is obvious, EditViewModel is the view model for my Edit action, and updatedCategory is an EF4 object. But I do not think this is relevant to the problem.
ok ... I just realized what I was doing was not necessary, because, regardless of whether I return to the "Edit" action with the updated category, I do not need to check whether the model is valid. I'm still curious to know the answer to the question if anyone can help.
asp.net-mvc actionresult
Derrick w
source share