asp mvc http以对象作为参数获取动作

时间:2022-09-25 14:43:25

In my controller I got action:

在我的控制器中我得到了行动:

[HttpGet]
public ActionResult CreateAdmin(object routeValues = null)
{
    //some code
    return View();
}

And http post:

和http帖子:

[HttpPost]
 public ActionResult CreateAdmin(
    string email, 
    string nameF, 
    string nameL, 
    string nameM)
 {
        if (email == "" || nameF == "" || nameL == "" || nameM == "")
        {
            return RedirectToAction("CreateAdmin", new
            {
                error = true,
                email = email,
                nameF = nameF,
                nameL = nameL,
                nameM = nameM,
            });
  }

variable routeValues in http get Action is always empty. How correctly pass object as parameter to [http get] Action?

http get中的变量routeValues始终为空。如何正确地将对象作为参数传递给[http get] Action?

3 个解决方案

#1


9  

You cannot pass an object to GET, instead try passing individual values like this:

您不能将对象传递给GET,而是尝试传递单个值,如下所示:

[HttpGet]
public ActionResult CreateAdmin(int value1, string value2, string value3)
{
    //some code
    var obj = new MyObject {property1 = value1; propety2 = value2; property3 = value3};
    return View();
}

You can then pass values from anywhere of your app like:

然后,您可以从应用的任何位置传递值,例如:

http://someurl.com/CreateAdmin?valu1=1&value2="hello"&value3="world"

#2


2  

As previously mentioned, you cannot pass an entire object to an HTTPGET Action. However, what I like to do when I do not want to have an Action with hundreds of parameters, is:

如前所述,您无法将整个对象传递给HTTPGET Action。但是,当我不想拥有包含数百个参数的Action时,我喜欢做的是:

  • Create a class that will encapsulate all the required parameters
  • 创建一个将封装所有必需参数的类

  • Pass a string value to my HTTPGET action
  • 将字符串值传递给我的HTTPGET操作

  • Convert the string parameter to the actual object that my GET action is using.
  • 将字符串参数转换为我的GET操作正在使用的实际对象。

Thus, say you have this class representing all your input fields:

因此,假设您有此类代表您的所有输入字段:

    public class AdminContract
    {
      public string email {get;set;}
      public string nameF {get;set;}
      public string nameL {get;set;}
      public string nameM {get;set;}
    }

You can then add a GET Action that will expect only one, string parameter:

然后,您可以添加一个只需要一个字符串参数的GET Action:

[HttpGet]
public ActionResult GetAdmin(string jsonData)
{
  //Convert your string parameter into your complext object
  var model = JsonConvert.DeserializeObject<AdminContract>(jsonData);

    //And now do whatever you want with the object
    var mail = model.email;
    var fullname = model.nameL + " " + model.nameF;

    // .. etc. etc. 

    return Json(fullname, JsonRequestBehavior.AllowGet);
}

And tada! Just by stringifying and object (on the front end) and then converting it (in the back end) you can still enjoy the benefits of the HTTPGET request while passing an entire object in the request instead of using tens of parameters.

和田田!只需通过字符串化和对象(在前端)然后转换它(在后端),您仍然可以在请求中传递整个对象而不是使用数十个参数时享受HTTPGET请求的好处。

#3


0  

You can pass values to a Http Get method like

您可以将值传递给Http Get方法,例如

Html.Action("ActionName","ControllerName",new {para1=value, para2 = value});

and you can have your action defined in the controller like

你可以在控制器中定义你的动作

[HtttpGet]
public ActionResult ActionName(Para1Type para1, Para2Type para2)
{
}

#1


9  

You cannot pass an object to GET, instead try passing individual values like this:

您不能将对象传递给GET,而是尝试传递单个值,如下所示:

[HttpGet]
public ActionResult CreateAdmin(int value1, string value2, string value3)
{
    //some code
    var obj = new MyObject {property1 = value1; propety2 = value2; property3 = value3};
    return View();
}

You can then pass values from anywhere of your app like:

然后,您可以从应用的任何位置传递值,例如:

http://someurl.com/CreateAdmin?valu1=1&value2="hello"&value3="world"

#2


2  

As previously mentioned, you cannot pass an entire object to an HTTPGET Action. However, what I like to do when I do not want to have an Action with hundreds of parameters, is:

如前所述,您无法将整个对象传递给HTTPGET Action。但是,当我不想拥有包含数百个参数的Action时,我喜欢做的是:

  • Create a class that will encapsulate all the required parameters
  • 创建一个将封装所有必需参数的类

  • Pass a string value to my HTTPGET action
  • 将字符串值传递给我的HTTPGET操作

  • Convert the string parameter to the actual object that my GET action is using.
  • 将字符串参数转换为我的GET操作正在使用的实际对象。

Thus, say you have this class representing all your input fields:

因此,假设您有此类代表您的所有输入字段:

    public class AdminContract
    {
      public string email {get;set;}
      public string nameF {get;set;}
      public string nameL {get;set;}
      public string nameM {get;set;}
    }

You can then add a GET Action that will expect only one, string parameter:

然后,您可以添加一个只需要一个字符串参数的GET Action:

[HttpGet]
public ActionResult GetAdmin(string jsonData)
{
  //Convert your string parameter into your complext object
  var model = JsonConvert.DeserializeObject<AdminContract>(jsonData);

    //And now do whatever you want with the object
    var mail = model.email;
    var fullname = model.nameL + " " + model.nameF;

    // .. etc. etc. 

    return Json(fullname, JsonRequestBehavior.AllowGet);
}

And tada! Just by stringifying and object (on the front end) and then converting it (in the back end) you can still enjoy the benefits of the HTTPGET request while passing an entire object in the request instead of using tens of parameters.

和田田!只需通过字符串化和对象(在前端)然后转换它(在后端),您仍然可以在请求中传递整个对象而不是使用数十个参数时享受HTTPGET请求的好处。

#3


0  

You can pass values to a Http Get method like

您可以将值传递给Http Get方法,例如

Html.Action("ActionName","ControllerName",new {para1=value, para2 = value});

and you can have your action defined in the controller like

你可以在控制器中定义你的动作

[HtttpGet]
public ActionResult ActionName(Para1Type para1, Para2Type para2)
{
}