Razor syntax in javascript not working

Posted by Santosh.Impossible under ASP.NET MVC on 8/5/2014 | Points: 10 | Views : 1579 | Status : [Member] | Replies : 1
Hi WebGurus,

I am converting the following code present in webform (DemoView.aspx) to razor view (DemoView.cshtml)

Code:
<script type="text/javascript">
$(function () {
$("#listDetails").click(function () { location.href = "<%= Model.ListDetailsUrl %>"; });

$("#provideDetails").validate({
rules: {
<% bool isFirst=true; foreach (var q in Model.Details) { if (!isFirst) { %>,<% }; isFirst=false; %>
"answer-<%= q.SequenceNumber %>": {
required: true
<% if (q.ValueType == Demo.ValueType.Int) { %>,digits: true<% } %>
<% if (q.ValueType == Demo.ValueType.Decimal) { %>,number: true<% } %>
}
<% } %>
}
});
});
</script>

But I am getting errors while conversion.

Can any one help me in resolving the issue?

Thanks & Regards,
Santosh Kumar Patro

santosh kumar patro


Responses

Posted by: Sheonarayan on: 8/5/2014 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
Hi Santosh,

Instead of using <%= ServerVariables %> use @ServerVariables.

All server side code in Razor starts with @ and good thing is you do not need any closing character in Razor for server side code; Razor engine is smart enough to understand that where server side code is ending and HTML code is starting.

I would suggest to have variables for all the stuff you want to do inside <script> block and simply print (@variableName) the variable value in the script block instead of writing server side code in the <script>.

Hope this helps,
Thanks


Regards,
Sheo Narayan
http://www.dotnetfunda.com

Santosh.Impossible, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response