weixin_33691700 2015-05-28 11:40 采纳率: 0%
浏览 74

Viewbag未更新

Currently on the I have some inline javascript, which makes an ajax call to a partial view controller which should have updated the viewbag along with it. However this does not seem to be the case, the data seems to persist from the main view which is null because it was never set there and if it was set then the data would still persist(tested).

Here is my javascript ajax call.

  $.ajax({
            url: btn.data('action-url'),
            data: {
                id: btn.data('id')
            },
            type: 'GET',
            success: function (data) {
                //delete all panels before showing new ones
                $('.panel.panel-default').remove();

                //push the new panels into the view
                //$('#dash-content').html(data);

                //Construct the partial view to be input into the main view

                //Checks to see if browser supports templates
                if ('content' in document.createElement('template')) {

                    var widgetModel = @Html.Raw(Json.Encode(ViewBag.widgets));

                    for (var i = 0; i < widgetModel.length; i++) {
                        var clone = loadwidgets(widgetModel[i]); //This function is in an external js file
                        var inputDestination = document.querySelector('#col2');
                        inputDestination.appendChild(clone);
                        console.log(inputDestination);
                    }
                }

and here is the Action that it is calling.

  public ActionResult Dashboard(int? id)
    {
        ModelState.Clear();
        //get all widgets associated dashboard
        var getWidgetsQuery = (from widgets in db.widgets
                               where widgets.DashID == id
                               select widgets);

        ViewBag.widgets = getWidgetsQuery.ToList();


        return PartialView();
    }
  • 写回答

1条回答 默认 最新

  • weixin_33705053 2015-05-28 12:09
    关注

    Add an action to return the data i.e.

      public ActionResult DashboardJson(int? id)
        {
            //get all widgets associated dashboard
            var getWidgetsQuery = (from widgets in db.widgets
                                   where widgets.DashID == id
                                   select widgets);
    
            var widgets = getWidgetsQuery;
    
            return Json(widgets, JsonRequestBehavior.AllowGet);
        }
    

    Declare and serialize your model above the json call as you have done:

    var widgetModel = @Html.Raw(Json.Encode(ViewBag.widgets));
    

    Then within your success call simply re-assign it to the returned data:

    widgetModel = data;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加