2

I'm trying to build a simple page with a simple form with ASP.NET using a Razor page, and can't work out how to handle a particular post-back. In particular (through factors outside of my control) I'm getting a post-back with a single query parameter that is lowercase and kebab-case, in a regular MVC page I could use the FromQuery attribute, but it doesn't appear to work in this instance as with or without the attribute I get null passed to OnPostAsync each time. An example to illustrate this issue follows:

Example.cshtml

@page
@model my_namespace.Pages.ExampleModel
@{
    ViewData["Title"] = "Example Title";
}

<h2>Example</h2>

<form method="post">
    <!--- In actual code I don't have control of the name, so this is for illustrative purposes. --->
    <input type="text" name="kebabbed-name"/>
    <input type="submit" />
</form>

Example.cshtml.cs

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace my_namespace.Pages
{
    public class ExampleModel : PageModel
    {
        public async Task<IActionResult> OnGetAsync()
        {
            return Page();
        }

        // This method is hit as expected, but the parameter is always null.
        // Changing the input name to something like "sample" and this parameter to match works however.
        public async Task<IActionResult> OnPostAsync(string kebabbedName)
        {
            // Handling of the Post request
            return Page();
        }
}

So what I'm looking for is some way to process the postback with that kebabbed-name as a parameter - any solution would be welcomed.

2
  • but it doesn't appear to work in this instance be specific ... do you get any error or the model not binded properly?
    – Rahul
    Aug 6, 2019 at 13:06
  • I get null through as the value each time despite the method being hit each time. I've updated the question accordingly to make it more clear. Aug 6, 2019 at 13:07

1 Answer 1

3

It doesn't seem like Razor pages could process kebabbed-names automagically, however you can create a property in your PageModel class with a custom name that should bind to the postback value.

// For GET request
// [BindProperty(Name = "kebabbed-name", SupportsGet = true)]

// For POST request
[BindProperty(Name = "kebabbed-name")]
public string kebabbedName { get; set; }
3
  • That's annoying, though as you usually control them yourself I can see that it usually isn't an issue. I've tried your solution, and it works as intended. Thanks :) Aug 6, 2019 at 13:32
  • Yes, I don't see this as a common use case. It's probably only useful when requests to your back end come from a front end you have no control of.
    – Imantas
    Aug 7, 2019 at 7:52
  • That's basically the situation I'm in - a drop in control via javascript from a trusted external source controls the data to post. Aug 7, 2019 at 8:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.