How to pass input value via post on Asp.net core Razor

Asked

Viewed 1,870 times

1

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

In 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>

Na 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();
}
  • You are not using any asynchronous method, prefer to use public IActionResult OnPost

2 answers

0


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

 <input asp-for="Tickets.Identificador" class="form-control" name="id" id="txtBusca"/>
  • 1

    And type="Ubmit" ?

  • type="Submit" is the button to send the form to the server.

  • 1

    Valeu worked perfectly

  • Good! I edited the answer and includes an online example if you still have any questions. Att.

0

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" />
  • 1

    Thanks Vinicius Lima, thanks for the tips.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.