How to split long string by newline in asp.net mvc view template
I want split Javascript code by newlines, but the compiler give开发者_开发百科s me an error:
<%= Html.ActionLink("Delete", "delete", new { id = Model.Id },
new {
@class="button-link",
onclick = " javascript;
javascript goes here;
javascript goes here;
javascript goes here;
return false;"
}
); %>
You could use a verbatim string literal -- starting the string with the @
symbol -- but it probably makes more sense to move your JavaScript out into a separate .js
file, as Darin suggests.
<%= Html.ActionLink("Delete", "delete", new { id = Model.Id },
new {
@class = "button-link",
onclick = @"javascript;
javascript goes here;
javascript goes here;
javascript goes here;
return false;"
});
%>
Not directly answering your question but proposing an alternative: javascript has nothing to do in HTML and both should never be mixed. It should be in a separate file:
<%= Html.ActionLink(
"Delete", "delete", new { id = Model.Id },
new { @class = "button-link", id = "foo" }); %>
and then in a separate js file (using jquery):
$(function() {
$('#foo').click(function() {
// TODO: put as many lines of javascript as you wish here
return false;
});
});
This way your markup is smaller, static resources such as javascript is cached by the client browser and you don't need to worry about simple, double, triple quotes, ...
精彩评论