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>)
}
public class JavaScriptHelper { public HelperResult Minify(Func<HelperResult> code) { return new HelperResult(writer => writer.Write(JavaScriptCompressor.Compress(code().ToString())));
Share
The helper needs to be static and Razor will pass in
Func<dynamic, HelperResult>rather than justFunc<HelperResult>. Also, you won’t want to pass<script></script>to the minifier so move them outside theJavaScriptHelper.Minify(...)call and then wrap the content with<text></text>so Razor knows how to parse it. Try this: