简体   繁体   English

将GET请求映射到Action

[英]Map GET request to Action

I'm trying to validate the progress quantity (and other fields once this works) that belongs to the BeginCollectionItems server side. 我正在尝试验证属于BeginCollectionItems服务器端的进度数量(以及一旦工作的其他字段)。 The request is being sent but the parameter progressQty is not being read by the action. 正在发送请求,但操作未读取参数progressQty。

This is the action I'm trying to map to: 这是我要映射到的操作:

    [AllowAnonymous]
    [AcceptVerbs("Get", "Post")]
    public IActionResult CheckValidProgressQty(int progressQty)
    {

        int a =progressQty;
        var result = false;

        if (a > 0)
            result = true;

        return Json(result);
    }

This is the request: 这是请求:

:method: GET :path: /Components/CheckValidProgressQty?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-300 :方法:GET:路径:/Components/CheckValidProgressQty?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-300

This is the Query String Parameters: 这是查询字符串参数:

ProgressItems[16bad1f2-155c-4a29-844c-34e88da80b7c].ProgressQty: -300 ProgressItems [16bad1f2-155c-4a29-844c-34e88da80b7c]。进度数量:-300

Here is the remote validation in the View Model Class: 这是视图模型类中的远程验证:

[Remote(action: "CheckValidProgressQty", controller: "Components", HttpMethod ="GET", ErrorMessage = "BAD QTY!")] public int ProgressQty { get; [远程(操作:“ CheckValidProgressQty”,控制器:“ Components”,HttpMethod =“ GET”,ErrorMessage =“ BAD QTY!”)] public int ProgressQty {get; set; 组; } }

Right now it goes into the CheckValidProgressQty method but I'm just not able to access the progressQty parameter. 现在,它进入CheckValidProgressQty方法,但是我只是无法访问progressQty参数。 One way I can access is: 我可以访问的一种方法是:

Request.QueryString.Value Request.QueryString.Value

?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-8 ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty = -8

and parse it. 并解析它。 But I think there should be something more simple available. 但我认为应该有一些更简单的方法。

ProgressItems[16bad1f2-155c-4a29-844c-34e88da80b7c].ProgressQty: -300 ProgressItems [16bad1f2-155c-4a29-844c-34e88da80b7c]。进度数量:-300

This is posted form data when you do POST method not for GET method. 当您执行POST方法而不是GET方法时,这是发布的表单数据。

You could not get the query string on your action parameters using ?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-300 since they are not match. 您无法使用?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-300获取有关操作参数的查询字符串,因为它们不匹配。

Refer to my below demo which introduces how to pass querystring to action, assume that I have models: 请参阅下面的演示,其中介绍了如何将querystring传递给操作,并假设我有模型:

public class TestUser
{
    [Key]
    public int Id { set; get; }
    public string Name { get; set; }
    public IList<UserInterest> Interests
    {
        get; set;
    }
}

public class UserInterest
{
    [Key]
    public int Id { set; get; }
    [Required]
    public string InterestText { set; get; }
    public int Option { set; get; }
}

You need to use an object like 您需要使用类似

public ActionResult UserTest(TestUser model)

And the querystring is ?Interests[0].InterestText=hello 而查询字符串是?Interests[0].InterestText=hello

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

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