代码之家  ›  专栏  ›  技术社区  ›  Eldar

如何在asp.net mvc视图模板中按换行拆分长字符串

  •  0
  • Eldar  · 技术社区  · 14 年前

    我想要用换行符拆分Javascript代码,但是编译器给了我一个错误:

    <%= 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;"
            }
        ); %>
    
    2 回复  |  直到 13 年前
        1
  •  2
  •   Community CDub    7 年前

    你可以用一个 verbatim string literal --以开始字符串 @ 符号——但是将JavaScript移到一个单独的 .js 文件, 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;"
                            });
    %>
    
        2
  •  1
  •   Darin Dimitrov    14 年前

    不是直接回答你的问题,而是提出一个替代方案:javascript与HTML无关,两者不应该混合使用。它应该放在一个单独的文件中:

    <%= Html.ActionLink(
        "Delete", "delete", new { id = Model.Id }, 
        new { @class = "button-link", id = "foo" }); %>
    

    然后在一个 js文件(使用jquery):

    $(function() {
        $('#foo').click(function() {
            // TODO: put as many lines of javascript as you wish here
            return false;
        });
    });
    

    这样你的标记就更小了,客户端浏览器缓存了javascript等静态资源,你不需要担心简单的、双引号、三引号。。。