Why did i get Status Code: 405; Method Not Allowed after trying to execute the delete action? - asp.net-core

ASP.NET Core 5.0, EF Core 5.0
My action :
[HttpPost("id")]
public IActionResult DeleteEquipment(int id)
{
repository.DeleteEquipment(id);
return View("WorkoutDB");
}
My ef method:
public void DeleteEquipment(int Id)
{
_context.Remove(new Equipment() { EquipmentId = Id });
_context.SaveChanges();
}
My button from partial view:
<input type="button" value="Delete" class="btn btn-danger" onclick="location.href='#Url.Action("DeleteEquipment", "Admin", item.EquipmentId)'" /
I don't know is it normal code or not, i am newbie, but i spent 4 hours and couldn't get the result.
Thanks for you attention!
UPD:
I changed my button
<td>
<form asp-action="DeleteEquipment" method="post">
<input type="hidden" name="EquipmentId" value="#item.EquipmentId" />
<button type="submit" class="btn btn-danger btn-sm">
Delete
</button>
</form>
</td>
But its still not working. I cant bind correct value to EquipmentId. Always 0 value. Why?

location.href='#Url.Action("DeleteEquipment", "Admin", item.EquipmentId)' cause browser to redirect user to new url. Redirect cause GET Request but your DeleteEquipment method has HttpPost attribute.
Replace
[HttpPost("id")]
with
[HttpGet("{id}")]
As #mason mentioned if you need to call your api with POST Request you need to use form.
<form asp-action="DeleteEquipment" asp-controller="Admin" asp-route-id="#item.EquipmentId">
<input type="submit" class="btn btn-danger" value="Delete" />
</form>

Related

The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST in laravel 8

I get the following error with the code below.
The DELETE method is not supported for this route. Supported methods:
GET, HEAD, POST
Controller
public function destroy(Post $post)
{
$post->delete();
return back();
}
Route
Route::delete('/posts/{post}', [PostController::class, 'destroy']);
View
<form action="{{ route('posts', $post) }}" method="POST" >
#csrf
#method('delete')
<button type="submit" class="text-blue-500">Delete</button>
</form>
Route:
Route::delete('/admin/user_list/{id}', [UserController::class, 'destroy'])->name('admin.user_list');
Controller:
public function destroy(Request $request, $id)
{
User::where('id', $id)->delete();
return redirect()->back()->withSuccess('Your record deleted successfuly');
}
View:
form action="{{ route('admin.user_list', $rows->id) }}" method="post"
#method('DELETE')
#csrf
<button onclick="return confirm('Are you sure you want to delete this?');" type="submit" value="delete" class="btn btn-danger btn-xs">
<span>DELETE</span>
</button>

onget handler razor pages

I have a button on a GET Form on asp.net core razor pages
<form method="get" asp-page="Index">
<button type="submit" asp-page="Index" asp-page-handler="Something" class="btn btn-primary"></button>
</form>
and the code behind
public IActionResult OnGetSomething(){
//... some code
return Page();
}
My problem is the onget handler code is never executed
If the form is POST the onpost handler will work fine but if it is GET it doesn’t work
So what am I missing here? and how to make the onget handler work?
When you submit a form using GET, the browser trashes query string values in the form action and replaces them with a new query string built from the form fields. You can hit your handler by adding an appropriate hidden field to the form:
<form method="get" asp-page="Index">
<input type="hidden" name="handler" value="Something" />
<button type="submit" class="btn btn-primary">Get</button>
</form>
you can't go to get method because of parameters miss matching.
Suppose:
In controller
[HttpGet]
public IActionResult Create()
{
return View();
}
//Post Method
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(string Name)
{
}
In View:
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>]
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
Then it go post method cause it has 1 parameters (Name). if we not net any input or parameters are not same then it hit different method.
It the concept of method overloading
In details of method overloading https://www.geeksforgeeks.org/c-sharp-method-overloading/

Nothing happens when clicking search button on bootsrap and .net core

I am trying to learn .net core and what I want to do is: I have a search bar and a GetDetail function in controller. I want to trigger that function when clicking search button according to text in form. But when I click button, nothing happens. I did not complete the trigger part but even if I simply redirect to another page with href, also nothing happens. Here is my search bar view code which taken from sbadmin2:
<div class="row justify-content-center">
<div class="col-6 p-3">
<div class="input-group">
<input type="text" class="form-control bg-light border-0 small" placeholder="Input" aria-label="Search" aria-describedby="basic-addon2" id="code">
<button class="btn btn-primary" type="button" action="/Books/GetDetail?">
<i class="fas fa-search fa-sm"></i>
</button>
</div>
</div>
</div>
and the function I want to trigger is a get request:
public IActionResult GetDetail(string codes)
{
var request = $"?codes={codes}";
var products = _httpTool.HttpGetAsync<List<Products>>($"{AppSettings.ApiUrl}/GetProducts{request}");
var productList = products.Result.ToList();
return Json(productList);
}
Why nothing is happening and how can I achieve this?
Thanks in advance!
try something like this:-
<form method="post" asp-action="GetDetail">
//clarify code
<input type="submit" value="Search" class="btn btn-outline-primary mt-1"/>
//clarify code
</form>
[HttpPost]
public IActionResult GetDetail(string codes)
{
var request = $"?codes={codes}";
var products = _httpTool.HttpGetAsync<List<Products>>($"{AppSettings.ApiUrl}/GetProducts{request}");
var productList = products.Result.ToList();
return Json(productList);
}
After clicking Searchyou see something will happen.

How to create object and send it to OnPost in Razor Pages?

How to create object and send it to OnPost in Razor Pages?
<form method="post">
<div>
<label>Add Text</label>
<input asp-for=SentimentData.SentimentText class="form-control" />
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
public void OnPost(SentimentData SentimentData)
{
Text = SentimentData.SentimentText;
Analysis.Evaluate(mlContext, Model, SplitDataView.TestSet);
Analysis.UseModelWithSingleItem(mlContext, SentimentData, Model, Text);
}
But after my input field SentimentText is null. i dont know why.

Razor Pages - Run a server side method NOT using post

In Razor pages ASP.NET Core, how do I do a basic onclick event for a button which is of type button?
Do I need to wire up an AJAX GET request to get the below "Resend Code" button to work? There is plenty of chatter about OnPost this and that.. but I don't want to post.
Can't be this hard?
<form method="post">
<div asp-validation-summary="All"></div>
<div class="form-group-item">
<label asp-for="Input.TwoFactorCode"></label>
<input asp-for="Input.TwoFactorCode" class="input" />
<span asp-validation-for="Input.TwoFactorCode"></span>
</div>
<div class="form-group-item">
<label class="margin-0" asp-for="Input.RememberMachine">
<input asp-for="Input.RememberMachine" />
#Html.DisplayNameFor(m => m.Input.RememberMachine)
</label>
</div>
<div>
<button type="button" asp-page-handler="ResendCode" class="btn btn-light">Resend Code</button>
<button type="submit" class="btn btn-secondary">Confirm</button>
</div>
</form>
As it stands, the button won't do anything. You can use JavaScript to intercept the button click and then fire a get request using AJAX (jQuery example below):
$('.btn.btn-light').on('click', function(){
$.get('?handler=ResendCode', data, function(){
...
});
});
You can try changing the button to use formmethod="get":
<button type="submit" formmethod="get" asp-page-handler="ResendCode" class="btn btn-light">Resend Code</button>
Note, this will only work for buttons that have type="submit" or type="image" (other type-values don't cause the form to submit). Also it's an HTML5 attribute.
Reference:
https://html.com/attributes/input-formmethod/
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formmethod
maybe try making your own custom handler methods other than OnPost() and OnGet()