简体   繁体   English

从ASP.NET MVC中的AngularJS控制器加载数据而无需加载整个页面

[英]Load data from AngularJS controller in ASP.NET MVC without loading entire page

Here is my Action: 这是我的动作:

    [HttpPost, ValidateAntiForgeryToken]
    public ActionResult GetData()
    {
            //get data from DB and populate model with data
            return View("Data", model);
    }

The view is: 视图是:

    @model UI.Models.DataModel

    @{
var grid = new WebGrid(Model.listTest, 
    null, 
    defaultSort: "Id", 
    rowsPerPage: 25, 
    canPage: true, 
    canSort: true
    );
    }

    @if (Model.listTest != null)
    {

     @grid.GetHtml()

    }

In this particular case, how can one populate the gridview without loading entire page after the action is called using angularjs (not Ajax)? 在这种情况下,如何使用angularjs(不是Ajax)调用动作后填充整个网格视图而不加载整个页面?

I have not written a single line of code for making it asynchronous because I don't know anything about angularjs. 我没有编写任何一行代码使其异步,因为我对angularjs一无所知。 This will be my first step towards learning it. 这将是我迈向学习的第一步。

Update: 更新:

  • I know how this can be done in Ajax, I wanted to learn AngularJS. 我知道如何在Ajax中做到这一点,我想学习AngularJS。 Is it better to do such things in Ajax only? 仅在Ajax中做这样的事情更好吗? Or AngularJS is better than Ajax? 还是AngularJS比Ajax更好?

If you're looking to have asynchronous API calls back to a controller, I highly recommend looking at the ASP.NET website, which contains information on this subject specifically. 如果您希望异步API回调到控制器,我强烈建议您访问ASP.NET网站,该网站专门包含有关此主题的信息。

The simplest answer before I link to an article is that you will want to split up your controller actions to use one for the View specifically, and another action which returns the data asynchronously. 在链接到文章之前,最简单的答案是,您将希望将控制器操作拆分为专门用于View,而另一操作则异步返回数据。

Here is a link to an article which answers your question: 这是回答您的问题的文章的链接:

http://www.asp.net/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4 http://www.asp.net/mvc/overview/performance/using-asynchronous-methods-in-aspnet-mvc-4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM