0

I'm trying to validate the progress quantity (and other fields once this works) that belongs to the BeginCollectionItems server side. The request is being sent but the parameter progressQty is not being read by the action.

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

This is the Query String Parameters:

ProgressItems[16bad1f2-155c-4a29-844c-34e88da80b7c].ProgressQty: -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; set; }

Right now it goes into the CheckValidProgressQty method but I'm just not able to access the progressQty parameter. One way I can access is:

Request.QueryString.Value

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

and parse it. But I think there should be something more simple available.

  • Could you share your view code and the view model?Have you tried to accept the data using object with [FromQuery] on parameters? – Ryan Jul 22 '19 at 02:28

1 Answers1

0

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

This is posted form data when you do POST method not for GET method.

You could not get the query string on your action parameters using ?ProgressItems%5B16bad1f2-155c-4a29-844c-34e88da80b7c%5D.ProgressQty=-300since they are not match.

Refer to my below demo which introduces how to pass querystring to action, assume that I have models:

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

Ryan
  • 19,118
  • 10
  • 37
  • 53