0

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?

2 Answers 2

2

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); } 
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.