views:

41

answers:

2

Hi there,

I tried to pass a string value into a JavaScript function like below:

<%= "'" + prop.property_description + "'") %>)

But it does not seems to be the best option, is there a better way to do the above without concatenate the string values with "'"?

Thanks

A: 

I happen to have an MVC application in front of me currently and I checked that this:

var test = "<%= Model.SearchTerm %>";

Results in the following output:

var test = "Example";

Which means you don't need to concatenate the quotes as you have in your example - you can place the entire C# block inside the quotes.

Sohnee
A: 

if your javascript function is being called inside an html tag ie <div onclick="javascriptfunction"> you can do the following initialize a new string variable using inline .net tags(<%=%>) like so <%=string strVar = "'" + prop.property_description + "'")%> then add it to your tag like so <div onclick="javascript:function(<%=strVar%>);"> this will avoid the ' conflict you can also whenever your dealing with strings and want to add " inside a string initialization just add \" to the string like so "john said \"Hi There \"" if you want your string to end until the end " is called add @ infront of your string init like so

@"Hello
  World"

This allows your string to span multiple lines without havig to add an extra + for every new line you want to format.

Ayo