如何防止Web Essential区域破坏jquery?

时间:2014-07-11 16:28:41

标签: javascript jquery ajax razor-2 web-essentials

我使用的是Web Essentials 2012和Visual Studio 2012以及Razor网页(不是MVC)。

我有一个看起来像这样的jquery ajax:

$("#test1").ready(function () {

    var test = { "loadTestList": "loadTestList" };

    function loadTestList(response) {
        var ddlTest = $('#test1');
        ddlTest.empty();

        ddlTest.append(
                $("<option></option>").text('All').val('All')
            );
        for (var i = 0; i < response.length; ++i) {
            ddlTest.append(
                $("<option></option>").text(response[i].TestName).val(response[i].TestName)
            );
        }
    }

    $.ajax({
        url: "../Controllers/TestController.cshtml",
        data: test,
        type: "POST",
        dataType: "json",
        success: function (response) {
            loadTestList(response);
        },
        error: function () {
            console.log("Sorry, there seems to be a problem contacting the Test server.");
            console.log(response.responseStatus);
            console.log(response.responseText);
        }
    });
});

这实际上有效。

现在我的控制器有这样的代码:

@* Controllers/TestController.cshtml *@
@{    
    if (IsPost && Request["loadTestList"] != null)
    {
        var tests = new TestRepository(new TestContext());
        var testslist = tests.Load();

        Json.Write(testslist, Response.Output);
    }
}@

是的,这也有效。

但是当我这样做时:

@{    
    <!-- #region TestMe -->
    if (IsPost && Request["loadTestList"] != null)
    {
        var tests = new TestRepository(new TestContext());
        var testslist = tests.Load();

        Json.Write(testslist, Response.Output);
    }
    <!-- #endregion -->
}@

每次都会破坏jQuery。 $ .ajax每次都会出错。

所以有人知道防止这种情况发生的方法吗?我真的很喜欢区域,但是我无法打破jQuery的其余部分。

另外,如果所有我传递的(通过Json.Write)都是testslist,为什么这会打破jQuery?

注意: TestController.cshtml只有服务器端代码。根本没有HTML。

1 个答案:

答案 0 :(得分:0)

在@ {}块中,您可以使用标准的c#预编译器指令:

#region [region_name]
#endregion

内部

<script></script>

阻止使用javaScript的web essentials模式:

//#region [region_name]
//#endregion
相关问题