❔ 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
Is it actually a controller action?
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
Show me the action method, at least the signature and attributes
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 });
}
And is it
[HttpGet]
?[HttpPost]
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
so i should change for <button> ?
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 Javascriptdamn was the [httget]
it worked now!
thank you so much
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.