C
C#2y ago
Henriquee

❔ Razor MVC

Hello, how do I pass a function that is in my controller to a button? I tried in the following way: <a [email protected] asp-action="QualificationEpaeDelete"> <i class="fa fa-trash"></i> </a> but it doesn't work, it takes me to a page with a 405 error
12 Replies
Angius
Angius2y ago
Is it actually a controller action?
Henriquee
HenriqueeOP2y ago
I believe so, I have a first form that serves to save the information from my inputs: <form id="formQualification" asp-action="QualificationEpaeSave" class="col-12 d-flex flex-column justify-content-start"> So, if there is already an item in the db with the same ID, I search and retrieve them through the Model, so I made a foreach to render them, and it's basically a crud, I made a delete function and I'm trying to apply it to my delete button, the only way I could make it work was by putting another form inside that foreach and doing the asp-action for my delete function, but due to having a form inside another form, there were some bugs, and then I found out that it is also a bad practice, so I would like to know if there is any way to put the function in my controller directly on my button
Angius
Angius2y ago
Show me the action method, at least the signature and attributes
Henriquee
HenriqueeOP2y ago
public async Task<IActionResult> QualificationEpaeDelete(int id, OpportunityQualificationEpaeViewModel model) { var result = await _opportunityQualificationEpaeService.DeleteAsync(id); FlashMessage.Success("CNPJ excluido com sucesso!"); return RedirectToAction(nameof(OpportunityQualificationEpae), new { opportunityId = model.OpportunityId }); }
Angius
Angius2y ago
And is it [HttpGet]?
Henriquee
HenriqueeOP2y ago
[HttpPost]
Angius
Angius2y ago
Because links, that is <a> elements, send a GET request exclusively Not to mention this route takes an ID, and some other model And you only have asp-route-id
Henriquee
HenriqueeOP2y ago
so i should change for <button> ?
Angius
Angius2y ago
Well, a button doesn't send any data whatsoever So that would work even less Either make this action a GET action and pass it two parameters, id and opportunityId Or use Javascript
Henriquee
HenriqueeOP2y ago
damn was the [httget] it worked now! thank you so much
Angius
Angius2y ago
Ok
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server