Donate

ASP.NET MVC ActionResult With [HttpPost, HttpPut] Filter Not Executing

Given the ActionResult method and View that executes the controller.
[HttpPost, HttpPut] 
public ActionResult Update(EditModel input)
{
	if (ModelState.IsValid)
	{
		var person = Database.People.Find(input.Id);
		input.ToPerson(person);

		Database.SaveChanges();

		return RedirectToAction("Edit", new { id = person.Id });
	}

	return View("Edit", input);
}
View:
@model PagedListTableMvcDemo.Models.EditModel  
 @{  
   ViewBag.Title = "Edit";  
 }  
 <h2>Update</h2>  
 @using (Html.BeginForm("Update", "People")) {  
   @Html.Partial("_Person")  
 }
When the user clicks the submit button rendered by the BeginForm helper, the browser shows an error URL not found or The view does not navigate to the Action with the corresponding Controller supplied in the BeginForm's parameters. The solution is to replace %5BHttpPost, HttpPut%5D with AcceptVerbs attribute.
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
public ActionResult Update(EditModel input)
{
	if (ModelState.IsValid)
	{
		var person = Database.People.Find(input.Id);
		input.ToPerson(person);

		Database.SaveChanges();

		return RedirectToAction("Edit", new { id = person.Id });
	}

	return View("Edit", input);
}

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.