jquery - 从 Controller 调用Viewbag数据来查看

标签 jquery ajax asp.net-mvc viewbag

Controller:-

[HttpPost]
        public ActionResult EmailScheduler()
        {
            long lCustDesignID = 1;
                int countProduct = gateWay.TotalCountOfCustomers(lCustDesignID);
                ViewBag.ItemCount = countProduct;

            return Json(JsonRequestBehavior.AllowGet);
        }

View:-

<h4>Number Of Records - <span>@ViewBag.ItemCount</span> </h4>

This controller is called on button click.

From Controller how to get value to View in viewbag.

.

最佳答案

如果EmailScheduler是ajax调用,那么你不能像你尝试过的那样使用ViewBag

您需要像下面这样修改您的代码。

Controller

[HttpPost]
public ActionResult EmailScheduler()
{
   long lCustDesignID = 1;
   int countProduct = gateWay.TotalCountOfCustomers(lCustDesignID);                    
   return Json(countProduct,JsonRequestBehavior.AllowGet);
}

HTML

<h4>Number Of Records - <span id="spnCount"></span> </h4>

Ajax

$.ajax({
   //....
   success: function(data){
      $('#spnCount').text(data);
   }
})

关于jquery - 从 Controller 调用Viewbag数据来查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44235543/

相关文章:

asp.net-mvc - 如何处理路由上的 null {id}?

javascript - 动态创建文件输入时 jQuery 文件上传不起作用

javascript - 如何在点击时加载/显示 asp.net 表单(面板)

javascript - 在 Ember 中使用 AJAX 发布后没有文件下载

javascript - 显示 JSON 数据

javascript - 动态拖放以构建单词

php - 在PHP脚本中调用mysql中的存储过程

javascript - 如何在 jquery 中构建 json 文件并从中提取数据值?

java - 为什么 AJAX 调用成功后 servlet 不转发?

asp.net - VS2012 中的 Entity Framework 问题 - 重命名属性不粘