如何在特定条件成立之前多次运行一个动作方法? [英] How can I run an action method multiple times until certain condition become true?

查看:72
本文介绍了如何在特定条件成立之前多次运行一个动作方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我在控制器中有一个动作方法如下:

Hi Everybody.
I have an action method in a controller as below:

public async Task<ActionResult> abc()
        {
            string nexturl = "this is a dynamic value"
            ViewBag.Data = nexturl;

            return View();
        }



上面的Action Method执行某些数据检查,然后将值传递给View并返回特定视图。在视图中如果我向下滚动到60px,我将被重定向到控制器中的另一个动作方法,如下所示:




Where above Action Method does some data checking then pass a value to the View and return a specific view. Within the view If I scroll down to 60px I'll be redirected to another action method in controller which is as below:

public async Task<ActionResult> abcGetMoreData(string nexturl)
        {
            if (nexturl != null)
            {
                // somme logic
            }
            ViewBag.Data = nexturl;
            return View("abc");
        }



上面的操作方法采用一个参数,该参数由带有ajax的'abc'视图提供。然后做一些检查并将nexturl的新值发送回视图。如果用户滚动下一个60像素,他/她将重定向到相同的操作方法'abcGetMoreData'并继续,直到nextUrl变为空。

我想要的是让用户轻松做这件事。我想一次加载所有数据,因此用户不应向下滚动以获取更多数据。我怎样才能实现这个目标?



我的尝试:



我没有尝试任何东西,因为我对C#和MVC都不熟悉。


Above action method takes a parameter which is fed by 'abc' view with ajax. then do some checking and sends back a new value for nexturl to the view. If the user scroll the next 60 px he/she will be redirect to the same action method 'abcGetMoreData' and continues until the nextUrl become null.
What I want is to make this thing easy for the user. I want to load all data in once so do user shouldn't scroll down to get more data. How can I achieve this?

What I have tried:

I haven't tried anything as far as I am very new to C# and MVC.

推荐答案

你不需要两个单独的动作方法。

只需定义一个,包含所需的所有参数,并在用户控制同一操作时触发AJAX事件。此操作将返回每个用户事件的预设数量的记录,即滚动60px。

您不需要多次运行它,只需重复用户操作。
You do not need two separate action methods.
Just define one, with all the parameters required, and fire a AJAX event as the user controls to the same action. This action will return the pre-set number of records per user event i.e. scrolling 60px.
You don't need to run it multiple times, just when the user action is repeated.


这篇关于如何在特定条件成立之前多次运行一个动作方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆