I'm using mvc3 with Razor Engine and I have a condition that when it's true should redirect automaticaly to another page sending an object as parameter. What I want to do is:
@if (cond > 10) {
Url.Action("Edit", product); //redirect in this line automaticaly to the action Edit sending the object as parameter }
Somebody knows if exist a Html.Helper that do this for me? I mean redirect to a page like this: someHtmlHelper("myAction", "myController", myObject).
I tried do this using JavaScript and almost works, I just find some problems in url parameter. The Url.Action returns the symbol code instead the caracter for the special caracters. Like "&" instead "&".
@if (cond > 10) { var url = Url.Action("Edit", product);
var Url = '@url'; location.href = Url;
<}
The url result is: http://localhost:4772/Product/Edit?ProductId=2234&Code=0020582&Name ...
I will be injured if MVC3 doesn't has a single page redirect implemented.
So I need help. Thanks.
The view isn't the appropriate place for that sort of logic.
Depending on your solution you should either:
A) Check the condition in yor Controller, prior to returning the View, and redirect there like so:
if (cond >= 10)
return RedirectToAction("ADifferentResult");
B) Use partial views to conditionally RenderAction like:
@{
if (cond > 10)
Html.RenderAction("ADifferentResult");
else
Html.RenderAction("DefaultResult");
}
@if (cond > 10){@Html.ActionLink("Edit", "Product", new { ProductId = 2234, Code = 0020582 })}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.