Web api: How to get the shape and set it for asp-action
-
In fact, there is a method that works perfectly if you write a request manually or with an annex:
// GET: api/contractor/showContractor/1 [HttpGet("ShowContractor/{id:int}")] public IActionResult ShowContractor(int id) { var contractor = GetContractor(id).Result.Value; return View(contractor); }
The button below also works perfectly:
<a asp-controller="Contractor" asp-action="ShowContractor" asp-route-id="1" class="button">Показать</a>
But there's no way I can make her work with the field, that's, that's, that's, that's, uh, that's, uh, that's, uh, that's, uh, that's, uh, that's, uh, that's, uh, that's, uh, that's the meaning that's being put under the id.
<div class="container"> <form method="get" asp-controller="Contractor" asp-action="ShowContractor"> <label>Исполнитель №</label> <input type="number" name="id"/> <input class="button" type="submit" value="Показать"/> </form> </div>
Please help me to the tail and to the mushroom, and the show doesn't work.
-
To transfer any form on the page that the user can choose. The submission in the first line of the code always indicates the model with which the presentation will work. For your case, it's probably a Contractor model. So it is first necessary to establish a form in which Id is filled for a copy of the Contractor facility. Furthermore, a POST-type method should be developed to which the Id model is transmitted and a GET type method with such a copy of the facility is produced. For your task, to avoid confusion, you can change the signatures of methods.
public IActionResult ShowContractorGet(Contractor _contractor) { // код для извлечения объекта из БД var contractor = new Contractor(); contractor.Id = _contractor.Id; contractor.Name = "Test"; return View(contractor);
}
Further Atribut
[HttpPost]
and the method of post to be raised from the submissionpublic IActionResult ShowContractorPost(Contractor _contractor)
{
return RedirectToAction("ShowContractorGet", _contractor);
}
The submission should clearly indicate that this is a form for data entry, adding a line
form method="post"
<div class="container">
<form method="post" asp-controller="Contractor" asp-action="ShowContractorPost">
<div>
<label asp-for ="Id"></label>
</div>
<div>
<input asp-for="Id" class="form-control"/>
</div><input class="button" type="submit" value="Показать" />
</form>
However, this approach, when you give the user the right to choose an arbitrary Id, including a non-existent object, is not the best practice. It is more appropriate to first set up the method and its presentation, which lists all existing facilities. The user may then choose an arbitrary, existing facility, with Id being hidden from the facility, but it is accessible to transfer its value if the facility is chosen and further worked with it. This is the approach to implementation https://ru.wikipedia.org/wiki/CRUD - Operatives. https://github.com/AndyBrizhak/Rocky with the operating code for Annex with ASP.NET Core MVC at .NET 5.
If you want to use WEB-API for your tasks, you can see a similar approach in this. https://github.com/AndyBrizhak/Mango with the operating code where the main front-end annex is based on ASP.NET Core MVC, and microservices are made from WEB-API for NET 6.