我正在尝试进行ajax调用以获取数据并修改DOM。

这是我的控制器/动作看起来像

   [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调用来看,它不是从局部视图中追加脚本元素。

我从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);

09-10 11:26
查看更多