简体   繁体   English

ASP.NET MVC:尝试将变量传递给另一个操作/方法

[英]ASP.NET MVC: Trying to pass a variable to another Action/Method

I am from Classic ASP background. 我来自经典ASP背景。 I am just trying to declare a variable int TOTAL_ROWS at the top and after I know how many rows in my MySQL database at the bottom, I will assign it to the variable and pass the value to another Action/Method to be used. 我只是想在顶部声明一个变量int TOTAL_ROWS ,在知道底部MySQL数据库中有多少行之后,我将其分配给该变量并将值传递给要使用的另一个Action / Method。 But I keep getting this An object reference is required for the non-static field I am a beginner for OOP. 但是我一直在得到这个信息An object reference is required for the non-static field 。我是OOP的初学者。 Please help. 请帮忙。

CODE

namespace BRO.Controllers
{
    public class PasswordController : Controller
    {
        int TOTAL_ROWS;

        //private const int TOTAL_ROWS = 2;
        private static readonly List<DataItem> _data = CreateData();

    private static List<DataItem> CreateData()
    {

        List<DataItem> list = new List<DataItem>();

        string mainconn = ConfigurationManager.ConnectionStrings["MySQLConnection"].ConnectionString;
        MySqlConnection mysqlconn = new MySqlConnection(mainconn);
        string sSQL = " SELECT * FROM mainpass ";
        MySqlCommand comm = new MySqlCommand(sSQL);
        comm.Connection = mysqlconn;

        MySqlDataAdapter adapter = new MySqlDataAdapter(comm);
        DataTable dt = new DataTable();
        adapter.Fill(dt);

        //===Try 1 Not working === An object reference is required for the non-static field,
        //============ method, or property ControllerBase.TempData
        TempData["TOTAL_ROWS"] = dt.Rows.Count;

        int iTOTAL_ROWS = dt.Rows.Count;
        //===Try 2 Not Working=== An object reference is required for the non-static field,
        //============ method, or property PasswordController.TOTAL_ROWS
        TOTAL_ROWS = iTOTAL_ROWS; //=== 

    public ActionResult AjaxGetJsonData(int draw, int start, int length)
    {
        string search = Request.QueryString["search[value]"];
        int sortColumn = -1;
        string sortDirection = "asc";
        if (length == -1)
        {
            length = TOTAL_ROWS; //*** Pass the value here
        }

        // note: we only sort one column at a time
        if (Request.QueryString["order[0][column]"] != null)
        {
            sortColumn = int.Parse(Request.QueryString["order[0][column]"]);
        }
        if (Request.QueryString["order[0][dir]"] != null)
        {
            sortDirection = Request.QueryString["order[0][dir]"];
        }

        DataTableData dataTableData = new DataTableData();
        dataTableData.draw = draw;
        dataTableData.recordsTotal = TOTAL_ROWS; //**** Pass the value here
        int recordsFiltered = 0;
        dataTableData.data = FilterData(ref recordsFiltered, start, length, search, sortColumn, sortDirection);
        dataTableData.recordsFiltered = recordsFiltered;

        return Json(dataTableData, JsonRequestBehavior.AllowGet);
    }

You have to make the TOTAL_ROWS a static variable. 您必须将TOTAL_ROWS设为静态变量。 Only then you will be able to set it from the static method. 只有这样,您才可以从静态方法进行设置。

But doing so will create a different problem if your system is used by multiple users at a time. 但是,如果一次由多个用户使用您的系统,这样做将产生另一个问题。 Users data would overlap - Concurrency issue. 用户数据会重叠-并发问题。

If you just want to pass data from one action to another then look in to the use of TempData object in Asp.net MVC here . 如果您只是想将数据从一个动作传递到另一个动作,请在此处查看 Asp.net MVC中TempData对象的TempData

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

相关问题 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller asp.net mvc使用表单将变量传递给另一个操作方法 - asp.net mvc pass a variable to another action method with a form post 如何将方法或参数传递给ASP.Net MVC中的操作筛选器 - How to pass a method or parameter to an action filter in ASP.Net MVC 将逗号分隔的值传递给ASP.NET MVC操作方法 - Pass Comma Separated Value to ASP.NET MVC Action Method 基于角色的安全性asp.net mvc尝试传递方法 - Role based security asp.net mvc Trying to pass a Method 将Model变量绑定到ASP.NET MVC3中的Action方法 - Binding the Model variable to an Action Method in ASP.NET MVC3 ASP.NET MVC 3-重定向到另一个操作 - ASP.NET MVC 3 - redirect to another action asp.net mvc 5异步动作方法 - asp.net mvc 5 asynchronous action method 将数据从一个控制器动作传递到ASP.Net MVC 5中的另一个控制器动作 - pass data from one controller action to another controller action in ASP.Net MVC 5 我想将可变表单控制器操作传递给视图 asp.net mvc - I want to pass a variable form controller action to a view asp.net mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM