When submitting a POST, GET, etc. request. You must name the input with the name of the parameter or property. For example:
<form method="post">
<input name="id" />
<input name="example" />
<input type="submit" value="Enviar" />
</form>
And then you can get the values as follows:
public IActionResult OnPost(int id, string example)
{
// ....
}
Or by using the BindProperty
attribute:
[BindProperty]
public int Id { get; set; }
[BindProperty]
public string Example { get; set; }
public IActionResult OnPost()
{
// ...
}
If you want to use the BindProperty
attribute also with requests of type GET , you must assign the SupportsGet
parameter to true
:
[BindProperty(SupportsGet = true)]
public int Id { get; set; }
You can also assign an object using .
(dot), as follows:
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
}
<form method="post">
<input name="Person.Name" />
<input name="Person.Surname" />
<input type="submit" value="Enviar" />
</form>
And then you can get the values as follows:
public IActionResult OnPost(Person person)
{
// ....
}
Or even using the BindProperty
attribute:
[BindProperty]
public Person Person { get; set; }
public IActionResult OnPost()
{
// ...
}
As the property is in PageModel
you can use the asp-for
attribute:
<input asp-for="Person.Name" />
Note: What matters is the name of the properties and parameters, not their type. That is, you could have a parameter of type Person
with name user
, and then in the input it would use like this: <input name="user.Name" />
.
Apparently you have a Tickets
property on your PageModel
. If you have it you can add the [BindProperty]
attribute to it, that all corresponding values of the POST request will be assigned and then could use this way:
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
// Agora você pode usar "Tickets.Identificador" ao invés de "id".
var item = _context.Tickets.Where(m => m.Identificador == Tickets.Identificador).First();
return RedirectToPage();
}
If you have, but it is already assigned, or has another purpose, you must name the input with the name of the parameter or property, which in your case is id
:
<input asp-for="Tickets.Identificador" class="form-control" name="id" id="txtBusca" />