关注此article后,我无法通过Partial View
加载Ajax
。但它没有加载partial view
。
注意:
<div id="UpdateTabData"></div>
标记为空alert('Test')
置于Ajax调用中的click事件中并使用breakpoint
内部控制器操作方法进行测试,Ajax确实正在调用TestAction方法并且该方法确实已返回return PartialView("PartialView", myViemodel);
在操作方法中没有任何错误。控制器:
[HttpGet]
public IActionResult TestAction(string calledFrom)
{
... some code here with a view model myViemodel
return PartialView("myPartialViewName", myViemodel);
}
查看:
@model myProj.Models.myTestViewModel
...some html here...
<div id="UpdateTabData"></div>
... more html here
...
视图末尾的Ajax代码:
$(document).ready(function () {
$('#myTabstripID li').click(function () {
var li_id = $(this).attr("id");
$.ajax({
url: '@Url.Action("TestAction", "myControllerName")',
data: { calledFrom: li_id},
contentType: 'application/json',
dataType: 'html'
type: 'GET',
cache: false,
success: function (data) {
$('#UpdateTabData').html(data);
},
error: function (data) {
alert('Error occurred');
}
});
});
});
答案 0 :(得分:0)
这是一个非常简单的任务(我的意思是获得一些返回局部视图的动作结果),所以它应该可行。所以你可能在某个地方有错误的id或类似的东西。
答案 1 :(得分:0)
您可以删除此部分代码。
contentType: 'application/json',
根据控制器中的ActionResult,您将返回部分视图。所以你的ajax调用期望一个视图而不是json。这将解决您的问题。