我正在尝试进行 ajax 调用来获取数据并修改 DOM。
这是我的 Controller /操作,看起来像
[HttpGet]
public ActionResult Index(int id)
{
// some code
return PartialView(Custom Object);
}
here is my Index View looks like
@Model CustomObject
@foreach(loop thru Model)
{
// add some other elements
}
if(Model.Count==0){
<script>some script</script>
}else{
<script>variable = '<div></div>'</script>
}
这是主视图代码
<script>var variable ='';</script>
<script>$('element').html(variable)</script>
<tbody id="tbodyId">
@RenderAction("Index");
<tbody>
这是我的 Ajax 调用。我试图做
function test(id){
$.ajax({
type:'Get',
url:'/Home/Index?id='+id,
datatype:'html'
})
.done(function(data){
$('#tbodyId').html(data);
});
}
我的问题是来自上面的 ajax 调用,它没有从部分 View 中附加脚本元素。
我从 javascript 方面尝试了不同的方法。但是,我不确定我这样做是否正确。因此,我需要来自线索的一些建议。
这是我尝试过的另一种方法。但运气不好
var scriptElement = document.createElement('script');
scriptElement.innerHTML = $('#tbodyId').last().html();
document.getElementById('tbodyId').appendChild(scriptElement);
如有任何帮助,我们将不胜感激。谢谢!!
最佳答案
尝试
var scriptElement = document.createElement('script');
scriptElement.textContent = $('#tbodyId').last().html();
// remove `#` from selector
document.getElementById('tbodyId').appendChild(scriptElement);
关于javascript - 如何进行ajax调用来获取包含javascript的部分 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32142179/