What is causing this mvc razor syntax error? - asp.net-mvc

var openWos = #Model.MyMaintenanceDashboard.OpenWorkOrders;
Why is Visual Studio saying this is a syntax error? It runs fine, but what would the proper syntax be?
Update: OpenWos is a javascript variable

If this is a javascript code, then you need to quote it so that you do not get the 'syntax' error.
var openWos = '#Model.MyMaintenanceDashboard.OpenWorkOrders';

If you're setting a C# var, the correct syntax would be without the # in front of Model as you're already in a C# code block:
#{
var openWos = Model.MyMaintenanceDashboard.OpenWorkOrders;
}
Edit:
You've clarified that this is a JavaScript variable. There shouldn't be a syntax error at all, as this is valid Razor. There is a distinct difference between quoting the value as suggested by Stephen, and the code that you posted originally. The type of the JavaScript variable is a string when quoted instead of a number.
For example, including this in a page:
<script>
#{ int z = 10; }
var x = #z;
console.log(x);
console.log(typeof x);
var y = '#z';
console.log(y);
console.log(typeof y);
</script>
Results in the following being logged to the console:
10
number
10
string
And there is no syntax error reported using the variable #z in either instance.

I've found that you can get around this by adding zero:
var openWos = #Model.MyMaintenanceDashboard.OpenWorkOrders + 0;

Related

Appending to lists inside ADT

I am trying to append to a list inside an ADT as follows:
data MyADT = myadt(list[str] s);
m = myadt([]);
m.s += "test";
Which causes an error:
|prompt:///|(0,3,<1,0>,<1,3>): Expected list[str], but got str
?[Advice](http://tutor.rascal-mpl.org/Errors/Static/UnexpectedType/UnexpectedType.html)
Which seems like it should work because this works:
x = [];
x += "test";
Maybe I am missing something here?
Thanks!
Try this as a workaround:
rascal>m.s += ["test"];
MyADT: myadt(["test"])
It looks like a bug.
By the way we will remove the overloading of + to add both elements and concatenate lists and introduce a special operator for adding elements. It's confusing as it is now.

action script 2.0 simple string comparing with conditional statement

So I have this crazy problem with comparing 2 strings in ActionScript 2.0
I have a global variable which holds some text (usually it is "statistic") from xml feed
_root.var_name = fields.firstChild.attributes.value;
when I trace() it it gives me the expected message
trace(_root.var_name); // echoes "statistik"
and when I try to use it in conditional statement the rest of code is not being executed because comparing :
if(_root.overskrift == "statistik"){
//do stuff
}
returns false!
I tried also with:
if(_root.overskrift.equals("statistik"))
but with the same result.
Any input will be appreciated.
Years later but on a legacy project I had the same problem and the solution was in a comment to this unanswered question. So let's make it an anwer:
Where var a:String="some harmless but in my case long string" and var b:String="some harmless but in my case long string" but (a==b) -> false the following works just fine: (a.indexOf(b)>=0) -> true. If the String were not found indexOf would give -1. Why the actual string comparison fails I coudn't figure out either. AS2 is ancient and almost obsolete...

loop through model in mvc razor code behind

I am working on MVC4 App, and I am stuck at one point, I tried using google to help me, but without success. This might be more simple then I think, but coming from web forms and shifting to mvc is "painful" sometime.
I am trying to loop through the model I have and get the values stored in that model. I tried few approaches but I am getting an error everytime. This is what I have:
var modelAgentFilter = from s in _aa.Agents
where s.COUNTER == Convert.ToInt32(AgentID)
select s;
if (modelAgentFilter != null)
{
ViewBag.FirstName = // Get FirstName object here
}
Thanks in advance for your comments.
Laziale
EDIT:
I did include for loop like this:
if (modelAgentFilter != null)
{
foreach (var property in modelAgentFilter)
{
string test = property.ADDRESS;
}
}
But when the compiler will reach the foreach step I am getting this error: "LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression."
I can get to the properties of the var model using that foreach look but as soon as the compiler will try to loop the model that error pops up.
Thanks again
LINQ to Entities does not recognize any methods. You can't use even ToString() in LINQ expression. You need first convert your value and than add it in LINQ.
In your example you need to do something like following:
var _agentID = int.Parse(AgentID);
var modelAgentFilter = from s in _aa.Agents
where s.COUNTER == _agentID
select s;

FirstOrDefault on List not working

I have a list like
var listDataDemo = model.Jobs.ToList();
The listDataDemo has data like following
Count = 13
[0]: {PaymentItemModel}
when i type in Immediate Window like
listData.FirstOrDefault()
it gives result
Amount: 0
Date: {1/01/0001 12:00:00 AM}
Method: "PayPal"
PayerName: null
PayerNumber: null
PaymentDetailType: Payment
TransactionId: null
But when i write(code in my class)
var demoVal = listData.FirstOrDefault(p=>p.Method=="PayPal")
The name 'demoVal' does not exist in the current context(no value)
How to get a value from LIST.
Please help me.
The below code is working fine for me. Just have a look:
List<test> testList = new List<test>();
testDB testObj = new testDB();
testList = testObj.fn_getAll();
var abc = testList.FirstOrDefault(a => a.Id == 3);
And you just try to change the name of the variable, this might be causing the issue.
I hope it will help you.. :)
Off the cuff, the predicate you're providing to FirstOrDefault to filter results looks like it has a syntax error: (p=>p.Method="PayPal") should be (p=>p.Method=="PayPal").
Granted this was probably a typo, for completeness:
'=' is an assignment operator, for when you want to assign a value to a variable.
'==' is an equality comparison operator, for when you want to test equality between values and get 'true' or 'false'.
Edited to answer beyond the typo...
Are you using Entity Framework?
EF has extension methods for FirstOrDefault also, except the filter parameter is an SQL query. If you're using EF, then you could be missing the following:
using System.Collections.Generic;
using System.Linq;
It looks like the predicate in your call to FirstOrDefault has a typo. You want == for comparison, not = for assignment.

how to use regex in MVC 3 with razor

when i use MVC 3 with razor it's work fine but when i write a regex using #section head{
} that they not work
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(#\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
it have # so that's give me error that
Parser Error Description: An error occurred during the parsing of a
resource required to service this request. Please review the following
specific parse error details and modify your source file
appropriately. Parser Error Message: "\" is not valid at the start of
a code block. Only identifiers, keywords, comments, "(" and "{" are
valid.
A neater option is to escape the # by placing another # in front of it (##)
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(##((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(##\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
Add <text></text> around the function, it tells Razor to not parse the contents:
<text>
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(#\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
</text>
Sorry, folks, you aren't reading the error, it's complaining about the backslashes, not the #, and or #{ } don't help.

Resources