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.
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; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.