Javascript 在.NET MVC中返回带有JSON参数的视图并更新DOM

Javascript 在.NET MVC中返回带有JSON参数的视图并更新DOM,javascript,jquery,asp.net,json,asp.net-mvc,Javascript,Jquery,Asp.net,Json,Asp.net Mvc,我有一个.NET操作,它基本上返回一组JSON结果,如下所示: return Json(new { lineData = groupedByDate, // more properties returned here... dataForTable = View("Index") // note this one }, JsonRequestBehavior.AllowGet); ViewBag.Items = items.ToList(); ($.post...).

我有一个.NET操作,它基本上返回一组JSON结果,如下所示:

 return Json(new
 {
   lineData = groupedByDate,
  // more properties returned here...
   dataForTable = View("Index") // note this one
  }, JsonRequestBehavior.AllowGet);
ViewBag.Items = items.ToList();
($.post...).done(function(data){
graph.SetData(data.lineData); // this is fine
And now I would like to simply inject the returned view here as well in the DOM.. Like this:

    var products= $('<table />').append(data.dataForTable).find('#tableProducts').html();
    $('#tableProducts').html(products);

});
在返回函数之前,我为我的ViewBag设置了数据源,它是我的HTML表格的数据源,如下所示:

 return Json(new
 {
   lineData = groupedByDate,
  // more properties returned here...
   dataForTable = View("Index") // note this one
  }, JsonRequestBehavior.AllowGet);
ViewBag.Items = items.ToList();
($.post...).done(function(data){
graph.SetData(data.lineData); // this is fine
And now I would like to simply inject the returned view here as well in the DOM.. Like this:

    var products= $('<table />').append(data.dataForTable).find('#tableProducts').html();
    $('#tableProducts').html(products);

});
在我看来:

<table id="tableProducts" class="table table-striped table-hover table-fw-widget">
       <tbody>
       @if (ViewBag.Items != null)
       {
        foreach (var item in ViewBag.Items)
        {
           <tr>
                  <td>@item.Title//for example</td>                                                                      
           </tr>
        }
       }
       </tbody>
</table>

@如果(ViewBag.Items!=null)
{
foreach(ViewBag.Items中的变量项)
{
@item.Title//例如
}
}
这是一个对我不起作用的部分。。。实际上,我正在post中的.done方法中设置json值,如下所示:

 return Json(new
 {
   lineData = groupedByDate,
  // more properties returned here...
   dataForTable = View("Index") // note this one
  }, JsonRequestBehavior.AllowGet);
ViewBag.Items = items.ToList();
($.post...).done(function(data){
graph.SetData(data.lineData); // this is fine
And now I would like to simply inject the returned view here as well in the DOM.. Like this:

    var products= $('<table />').append(data.dataForTable).find('#tableProducts').html();
    $('#tableProducts').html(products);

});
($.post…).done(函数(数据){
graph.SetData(data.lineData);//这很好
现在我想简单地将返回的视图注入到DOM中,如下所示:
var products=$('').append(data.dataForTable).find('#tableProducts').html();
$('#tableProducts').html(产品);
});
这就是问题所在:

 var products= $('<table />').append(data.dataForTable).find('#tableProducts').html();
        $('#tableProducts').html(products);
var-products=$('').append(data.dataForTable).find('#tableProducts').html();
$('#tableProducts').html(产品);
我希望将视图的返回部分注入DOM,但在执行此操作时没有发生任何事情。。。桌子还是空的,就像一开始一样


我做错了什么?

如果您想实现这种功能,那么可以使用以下流程:

1) 为html创建部分视图

2) 使用某些函数将部分视图转换为html字符串

3) 将该字符串传递到json参数中

4) 使用ajax调用方法

5) 在ajax成功中,您可以在任何需要的地方替换html字符串


检查ajax请求,看看从服务器返回的json是否符合您的预期。@Shyju它说:对象{MasterName:”,模型:null,TempData:Array(0),视图:null,ViewBag:Object…}MasterName:“模型:nullTempData:Array(0)视图:nullViewBag:ObjectViewData:Array(2)ViewEngineCollection:Array(2)ViewName:“索引”原型:Object@Shyju看起来视图为空?也许我应该声明视图的路径或类似的内容?有人吗???=)