Reputation: 151
I have the following:
@if ((@Model.SeqId != 0) & (Model.SeqId != 1))
{
<text>
window.location.href = "www.stackoverflow.com";
</text>
}
I don't know much about razor. Is there something I could do to make it simpler?
Upvotes: 0
Views: 318
Reputation: 3274
Can SeqId ever be less than 0? If not, you could do
@if (Model.SeqId > 1)
{
<text>
window.location.href = "www.stackoverflow.com";
</text>
}
Also, you don't need to @ the Model in a code block. You probably want to use && instead of & as this fails as soon as the first test is false, saves a few CPU cycles.
http://msdn.microsoft.com/en-us/library/2a723cdk(v=vs.71).aspx
Upvotes: 1
Reputation: 1039528
Yes, you can. Suffice to define a property on your view model
public bool ShouldRedirectToSO
{
get
{
return (SeqId != 0 && SeqId != 1);
}
}
and then:
<script type="text/javascript">
@if (Model.ShouldRedirectToSO)
{
@:window.location.href = 'http://www.stackoverflow.com';
}
</script>
or if you intend to redirect immediately on page load if the condition is met you could also do this directly from the controller:
public ActionResult Foo()
{
var model = ...
if (model.ShouldRedirectToSO)
{
return Redirect("http://www.stackoverflow.com");
}
return View(model);
}
Upvotes: 2