将GET请求映射到操作

时间:2019-07-21 16:02:34

标签: asp.net-core get remote-validation begincollectionitem

我正在尝试验证属于BeginCollectionItems服务器端的进度数量(一旦可行,便会验证其他字段)。正在发送请求,但操作未读取参数progressQty。

这是我要映射的操作:

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

        int a =progressQty;
        var result = false;

        if (a > 0)
            result = true;

        return Json(result);
    }

这是请求:

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

这是查询字符串参数:

ProgressItems [16bad1f2-155c-4a29-844c-34e88da80b7c] .ProgressQty:-300

这是View模型类中的远程验证:

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

现在,它进入CheckValidProgressQty方法,但我只是无法访问progressQty参数。我可以访问的一种方法是:

Request.QueryString.Value

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

并解析它。但我认为应该有一些更简单的方法。

1 个答案:

答案 0 :(得分:0)

  

ProgressItems [16bad1f2-155c-4a29-844c-34e88da80b7c] .ProgressQty:-300

当您使用POST方法而不是GET方法时,这是发布的表单数据。

您无法使用?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-300获取有关操作参数的查询字符串,因为它们不匹配。

请参阅下面的演示,其中介绍了如何将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; }
}

您需要使用类似对象

public ActionResult UserTest(TestUser model)

查询字符串为?Interests[0].InterestText=hello