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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵