locked
Remote Validation with Razor Pages - Error 400 RRS feed

  • Question

  • User1518025236 posted

    Hello every body.

    I am working on a project which is using Razor Pages

    I want to have [PageRemote] validation and read

    https://www.learnrazorpages.com/razor-pages/validation/remote-validation

    And

    https://forums.asp.net/t/2161254.aspx?Remote+Validation+with+Razor+Pages

    I followed the article step by step but again i faced 400

    here is my Code

     [PageRemote(
                    ErrorMessage = "Username already exists",
                    AdditionalFields = "__RequestVerificationToken",
                    HttpMethod = "post",
                    PageHandler = "CheckUsername"
                )]
                [BindProperty]
                [Display(Name = "Username")]
                public string Username { get; set; }

    And there is my method

       public JsonResult OnPostCheckUsername()
            {
                return new JsonResult(true);
            }

    How can I solve the problem ?

    Saturday, February 8, 2020 9:27 PM

Answers

  • User1518025236 posted

    Sorry please do not mention previous post.

    my problem was nested properties.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 8, 2020 9:48 PM

All replies

  • User1518025236 posted

    Sorry please do not mention previous post.

    my problem was nested properties.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 8, 2020 9:48 PM
  • User711641945 posted

    Hi Arashzandi,

    >my problem was nested properties.

    What do you mean for this problem?Could you share a simple demo that could reproduce the issue?Also could you share your error message?

    I guess that you may want to use [PageRemote] on property which is in a model.

    You could refer to:

    https://stackoverflow.com/questions/59463944/how-to-use-pageremote-in-razor-pages

    Best Regards,

    Rena

    Monday, February 10, 2020 6:00 AM
  • User-821857111 posted

    Rena Ni

    >my problem was nested properties.

    What do you mean for this problem

    A "nested property" is a property of a class that is a property of the PageModel. You may have a Contact class:

    public class Contact
    {
        public string EmailAddress { get; set; }
    }

    Then you may make that a bindable property of a PageModel:

    public class ContactPageModel
    {
        [BindProperty]
        public Contact Contact { get; set; }
        ...
    }

    When you use Tag helpers to generate the input for the Contact.EmailAddress property, the rendered name and id values are Contact.EmailAddress. The Contact prefix is also applied to every field listed in the AdditionalFields property of the PageRemote attribute, including the RequestVerificationToken field, which results in it not being located by the framework because the name of the posted field is not as expected. Therefore Razor Pages returns a 400 Bad Request code indicating request verification failure.

    The workaround is to add another property to the PageModel representing the email address. 

    public class ContactPageModel
    {
        [BindProperty]
        public Contact Contact { get; set; }
        [BindProperty, Remote(...)]
        public string EmailAddress { get; set; }

        ...
    }

    Monday, February 10, 2020 10:59 AM