【问题标题】:ASP MVC sb-admin 2 Bootstrap template: Panel content refreshASP MVC sb-admin 2 Bootstrap 模板:面板内容刷新
【发布时间】:2016-02-12 09:12:49
【问题描述】:

我想知道刷新面板内容的好方法是什么,例如,对于下面显示的任务面板。

值(任务数量)是通过 API “可请求的”。我想要做的是获取返回 JSON 数组(所有任务列表)的 uri/api/tasks 并更新任务数量。

我的Index.cshtml中对应的div如下:

<div class="col-lg-3 col-md-6">
            <div class="panel panel-green">
                <div class="panel-heading">
                    <div class="row">
                        <div class="col-xs-3">
                            <i class="fa fa-tasks fa-5x"></i>
                        </div>
                        <div class="col-xs-9 text-right">
                            <div class="huge">12</div>
                            <div>New Tasks!</div>
                        </div>
                    </div>
                </div>
                <a href="#">
                    <div class="panel-footer">
                        <span class="pull-left">View Details</span>
                        <span class="pull-right"><i class="fa fa-arrow-circle-right"></i></span>
                        <div class="clearfix"></div>
                    </div>
                </a>
            </div>
        </div>

不幸的是,我不知道将 API 调用放在哪里以及如何调用它们。

【问题讨论】:

  • 你需要使用signarR
  • 谢谢@janina,你能说得具体一点吗?你有这方面的例子吗?
  • 您希望多久更新一次?
  • 取决于。一些面板在仪表板加载时只加载一次,其他面板每次值更改时。为简单起见,目前只使用一次程序就足够了。

标签: c# asp.net asp.net-mvc twitter-bootstrap


【解决方案1】:

如果你想让事情变得简单,我会使用 ajax 和 jquery 的 onload 函数

用你的 c# 方法

//this will return a list of strings in json format.
public ActionResult GetData()
{
//array of string 
String[] array = {"Bob","Joe","Dave"};
 return json(array,JsonRequestBehaviour.AllowGet);
}

Then in the view 
<script>
$(document).ready(function(){
     $.ajax({
        url: "/Controller/GetData",

        method: 'GET',
        contentType: "application/json; charset=utf-8",
        success: function (result) {

        $.each(result,function(key,value){
        //append the dom in here for each result in the array.
        });

        },
        error: function (result) {
        }
});
});
</script>

【讨论】:

  • url: "/Controller/GetData" 可以指向任何控制器吗?我在我的项目控制器目录中创建了一个 TaskController。但它永远不会调用 TaskController!
  • 在这种情况下,它应该是 url:"/Task/GetData" 通知不是 TaskController,因为路由会为您处理这些
  • 我使用控制器作为占位符,如果你有另一个控制器说 PersonController,那么它就是 GetData 的 actionresult,url:"/Person/GetData"
  • 我在控制器中有这个:public class TasksController : Controller { public ActionResult Index() { var tasks = new List&lt;Tasks&gt;(); tasks.Add(new Tasks { amount = 4 }); return View(tasks); } 现在添加一些假数据(数量 = 4)。我通过url: "/Tasks/Index" 在面板内调用它。对吗?
  • 好的,谢谢! :-) 最后一个问题:&lt;script&gt;...&lt;/script&gt; 部分,我到底应该把它放在哪里?我是否必须在项目的 /Views/Tasks 中创建新的任务视图?这个视图是一个cshtml文件。
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 2015-05-28
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
相关资源
最近更新 更多