2012-02-18 49 views
0
public class JavaScriptHelper 
{ 
    public HelperResult Minify(Func<HelperResult> code) 
    { 
     return new HelperResult(writer => writer.Write(JavaScriptCompressor.Compress(code().ToString()))); 
    } 
} 

@section Script 
{ 
@JavaScriptHelper.Minify(
@<script> 
    (function ($, b) { 
     $(function() { 
      $("#upload").bind("submit", function (e) { 
       e.preventDefault(); 
       console.log("going"); 
       $(this).ajaxSubmit(function (result) { 
        if (!b.ajaxFailure(result, true)) { 
         console.log(result); 
        } 
       }); 
      }); 
     }); 
    })(jQuery, b); 
</script>) 
} 
+0

我希望我添加的標籤是正確的。如果沒有,請將它們改回。 – CodesInChaos 2012-02-18 23:46:43

回答

1

該幫手需要是靜態的,剃刀會通過Func<dynamic, HelperResult>而不僅僅是Func<HelperResult>。此外,您不希望將<script></script>傳遞給縮小器,因此請將其移動到JavaScriptHelper.Minify(...)調用之外,然後用<text></text>將其包含在內容中,以便剃刀知道如何解析它。試試這個:

public class JavaScriptHelper 
{ 
     public static HelperResult Minify(Func<dynamic, HelperResult> code) 
     { 
      return new HelperResult(writer => writer.Write(JavaScriptCompressor.Compress(code(null).ToString()))); 
     } 
} 

@section Script 
{ 
    <script> 
    @JavaScriptHelper.Minify(
    @<text> 
    (function ($, b) { 
     $(function() { 
      $('#upload').bind('submit', function (e) { 
       e.preventDefault(); 
       console.log('going'); 
       $(this).ajaxSubmit(function (result) { 
        if (!b.ajaxFailure(result, true)) { 
         console.log(result); 
        } 
       }); 
      }); 
     }); 
    })(jQuery, b); 

    </text>) 
    </script> 

} 
+0

謝謝,我最終使用了部分操作 – bevacqua 2012-02-19 15:52:32