我正在尝试将 html 从我的部分 View 之一获取到主视图中 ID 为“searchpage”的 div 标签中。但是,当我遵循此答案中的语法时, How to append an Html.Action to the jquery ,我在 chrome 开发者工具中收到错误,
Uncaught SyntaxError: Invalid or unexpected token
代码是
$("#searchbutton").click(function () {
$("#searchpage").html('@Html.Action("Search", "Employees")');
});
该错误在 ('
处特别突出显示。
最佳答案
我建议使用 Ajax 调用并加载响应:
$("#searchbutton").click(function () {
var url = '@Url.Action("Search", "Employees")';
$.get(url)
.done(function(response, status, jqxhr){
$("#searchpage").html(response);
});
});
如果您要加载静态表单,请将其加载到模板脚本中并根据需要进行复制:
<script type="text/template" id="search-template">
@Html.Action("Search", "Employees")
</script>
脚本中的某个位置:
$("#searchbutton").click(function () {
var template = $('#search-template').clone().html();
$("#searchpage").html(template);
});
我假设您看到的错误是因为您的 HTML 已格式化,因此您认为正在构建的“字符串”实际上在生成的标记中无效 - 它会有换行符,这是一个问题JavaScript 字符串。
关于jquery - 无法在 jQuery 中调用 @Html.Action(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44443838/