简体   繁体   English

ASP.NET MVC C#-将数据从控制器传递到类

[英]ASP.NET MVC C# - Pass Data from Controller to Class

I have two class Libraries, one for my "backend" Code and one for the WebInterface side of the project. 我有两个类库,一个用于我的“后端”代码,另一个用于项目的WebInterface端。

I'm passing two variables (UserId and EnvId) using Ajax/Jquery to my Controller in the WebInterface side of my project. 我正在使用Ajax / Jquery将两个变量(UserId和EnvId)传递到项目的WebInterface端的Controller中。 I then need to pass the two values from the Var's to the other Class Library to run several methods for unlocking user accounts. 然后,我需要将两个值从Var传递到另一个类库,以运行几种方法来解锁用户帐户。

Below is the code from my Controller (the code here currently post console.log messages back to the browser to I know I'm getting the right ID's) 以下是来自我的控制器的代码(此处的代码当前将console.log消息发布回浏览器,以使我知道我获得了正确的ID)

[HttpPost]
    public ActionResult UnlockUserAction(string UserId, string EnvId)
    {
        var user = UserId;
        var environment = EnvId;

        if (user == "" || user == "0")
        {

            return Json("Error - The User doesn't exist or there was an error", JsonRequestBehavior.AllowGet);
        }
        else
        {
            //pass userid & envid to UnlockUser Class will go here
            var result = user + " | " + environment;
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }

This is my code from the "backend" Class Library; 这是我来自“后端”类库的代码;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace UserImportInterfaceLibrary
{
    public class UnlockUser
    {
    //Get Environment SQL
    public string sql = "SELECT Server,Catalog,UserName,UserPwd FROM tbl_Environments WHERE Description = @environment";
    //Unlock User String
    public string unlockSQL = "UPDATE User_Account_Data SET Account_Locked = 0 WHERE User_ID = @userid";

    public void getEnvironment(EnvId)
    {
        var envData = EnvId;
    }
}  

} }

You can do this in multiple ways. 您可以通过多种方式执行此操作。 Either in the constructor of you class library or use it as a service by creating a parameter less constructor with a method that does your logic. 在类库的构造函数中,或者通过使用执行您的逻辑的方法创建较少参数的构造函数,将其用作服务。

In an ideal solution I would recommend splitting your code into three projects. 在理想的解决方案中,我建议将您的代码分成三个项目。 One that access' your data and gets everything you need. 一个可以访问您的数据并获得您所需的一切的人。 One that handles all your logic with the data received from data layer. 一种处理从数据层接收的数据的所有逻辑的方法。 Finally your MVC project that only deals with the logic layer. 最后,您的MVC项目仅涉及逻辑层。

To answer your question for now: 现在要回答您的问题:

public class UnlockUser
{
     public UnlockUser(string user, string env)
     {
         //Do logic with params
     }

}

In your controller add: 在您的控制器中添加:

    else
    {
        //pass userid & envid to UnlockUser Class will go here
        var foo = new UnlockUser(userId, envId);
        var result = user + " | " + environment;
        return Json(result, JsonRequestBehavior.AllowGet);
    }

OR: 要么:

Add parameter less constructor in your class and a method that takes your params and does some logic. 在类中添加较少参数的构造函数,并添加采用参数并执行一些逻辑的方法。 Use it as so: 如此使用:

public class UnlockUser
{
     public UnlockUser()
     { }
     public <YourResultObject> <YourNewMethod> (string userId, string envId)
     {
          //Do some logic
          return <YourResultObject>;
     }
}

In your controller method: 在您的控制器方法中:

var foo = new UnlockUser();
foo.YourNewMethod(userId, envId);`

You need to define a function in your class that will receive the parameter you need like: 您需要在类中定义一个函数,该函数将接收所需的参数,例如:

public void MyFunction(MyParameter){ //do anything with your parameter here }

And then in your controller you need to create an object of your class like: 然后,在控制器中,您需要创建类的对象,例如:

var user = new UnlockUser();

Finally you can call the function of your class in the controller 最后,您可以在控制器中调用您的类的函数

user.MyFunction(MyParameter);

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

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