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 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64