This is my controller,
I am trying to get data from this controller by using code below but I am getting .
Can you please tell me what am I doing wrong?
4 Answers 4
@Url.Action only returns the action url’s string, without quotes around it.
You’ll need to wrap that url in quotes.
Otherwise, the file returned to the client will contain:
Which isn’t valid JS, nor what you want. The replacement gives the following result:
Please remove the Controller suffix while specifying in url .
Try it this way:
If you interpolate a variable that begins with a slash but do not put quotation marks around it, it will be interpreted as a regular expression, not a string. Another time this comes up is with JSP expression language, where you should write the first, not the second:
I am getting the error
Invalid regular expression flags
From the edit button when I click on it. But HTML output seems to be fine
2 Answers 2
The error is in the code onclick=’location.href=" + directlink + "’ . Here, location.href is assigned a RegEx(Note that regex literal syntax uses / as delimiters).
Here, /Products/edit/11 is read as Regex /Products/ and edit as regex flags, which are Invalid flags(except i ).
To solve this, the value of the URL can simply be wrapped inside quotes. As both single and double quotes are already used, the quotes need to be escaped.
If target environment support EcmaScript 6 template literals, you can use
As, yo’re using jQuery, I’d suggest to use it to create new element and bind events on it.
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
It’s funny because it’s true, right? But we still have regular expressions, they’re still powerful, and we still use them.
The Use Case
Earlier this week, I was working on a project in which a user would be making a selection from a `select` box.
I had an event handler attached to `select` element’s `onchange` event so that when the user made their selection, the DOM would be manipulated in such a way that certain elements would have their spaces replaced with a hyphen.
Specifically, the original code looked something like this:
Now, up until a certain point in testing I had no problem with this. In fact, it was working fine in WebKit based browsers, but it tanked in Firefox.
Invalid Regular Expression Flag
Of course, this is really a manifestation of laziness or forgetfulness (I’d much rather assume the latter) on my part because the method signature for `replace` is `regex/substr, newString`.
As such, it required a change to the code as follows:
And thus, the problem was resolved.
So What’s The Deal?
- The first version of the code was saying “replace all substring spaces with a hyphen.”
- The second version has a regular expression that says “replace any occurrence of a space that occurs globally in the string.”
If regular expressions are the argument accepted in a function signature, then that’s the one you’ve gotta use. Unless, of course, you’re only building for WebKit based browsers, but that’s an entirely other post.