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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader