Ensure that the helper is static and that Razor will pass in
Func<dynamic, HelperResult>
, rather than just
Func<HelperResult>
. It's also important to exclude
<script></script>
from the minifier by moving them outside the
JavaScriptHelper.Minify(...)
call and then wrapping the content with
<text></text>
so Razor can correctly parse it. Here's an updated version of the code:
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>
}