How to pass input value via post in asp.net core razor

1

I would like to know how to pass the value typed in an input to a Post method in PageModel.

No cshtml:

<div class="col-sm-6">
    <label asp-for="Tickets.Identificador" class="control-label"></label>
    <input asp-for="Tickets.Identificador" class="form-control" id="txtBusca"/>
    <span asp-validation-for="Tickets.Identificador" class="text-danger"></span>
</div>
<br />
<div class="form-group">
    <input type="submit" asp-route-id="Tickets.Identificador" value="Localizar" class="btn btn-danger btn-sm" style="margin-top:5px;" />
</div>

In PageModel:

public async Task<IActionResult> OnPostAsync( string id)
{
    if (!ModelState.IsValid)
    {
        return Page();
    }

    var item = _context.Tickets.Where(m => m.Identificador == id).First();

    return RedirectToPage();
}
    
asked by anonymous 18.06.2018 / 21:48

2 answers

0

In your input put the property (name) with the same name of the parameter received in your action. Here's an example in dotnetfiddle

 <input asp-for="Tickets.Identificador" class="form-control" name="id" id="txtBusca"/>
    
18.06.2018 / 22:00
0

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" />
    
18.06.2018 / 22:35