When sending a POST, GET, etc. request, you should name the input with the parameter or property name. For example:
<form method="post">
<input name="id" />
<input name="example" />
<input type="submit" value="Enviar" />
</form>
And then you can take the figures as follows:
public IActionResult OnPost(int id, string example)
{
// ....
}
Or using the attribute BindProperty
:
[BindProperty]
public int Id { get; set; }
[BindProperty]
public string Example { get; set; }
public IActionResult OnPost()
{
// ...
}
If you want to use the attribute BindProperty
also with requests of the type GET, shall assign the parameter SupportsGet
with true
:
[BindProperty(SupportsGet = true)]
public int Id { get; set; }
You can also assign an object using .
(point), 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 take the figures as follows:
public IActionResult OnPost(Person person)
{
// ....
}
Or still using the attribute BindProperty
:
[BindProperty]
public Person Person { get; set; }
public IActionResult OnPost()
{
// ...
}
As the property is in PageModel
you can use the attribute asp-for
:
<input asp-for="Person.Name" />
Obs: What matters is the name of the properties and parameters, not their type. That is, you could have a type parameter Person
with the name user
, and then in input would use so: <input name="user.Name" />
.
Apparently you have a property Tickets
in his PageModel
. If you have it you can add the attribute [BindProperty]
for her, that all the corresponding values of the POST request will be assigned and then could use in 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 should name the input with the parameter or property name, which in your case is id
:
<input asp-for="Tickets.Identificador" class="form-control" name="id" id="txtBusca" />
You are not using any asynchronous method, prefer to use
public IActionResult OnPost
– Vinícius Lima