I am currently baffled to why my method is not running. I placed a breakpoint on it because I suspect some failure to occur somewhere.
Here is my explanation:
I just started a new ASP.NET MVC 2 application. I Connected my Home/Index page to a new strongly-typed view which I have created called Categories/List. Everything Displays fine – the categories are retrieved from my Entity Data model and placed on the page. I removed the “Details” link which is automatically created with the view.
Afterwards, I added a strongly Typed Delete View to Categories/Delete. In my CategoriesController, I have the following code (Which works)
<%: Html.ActionLink("Delete", "DeleteCategory", new { id=item.CategoryID })%>
to:
/// <summary>
/// Get rid of a Category
/// </summary>
/// <returns>Redirect to ~/Categories/Delete</returns>
public ActionResult DeleteCategory(int id)
{
ViewData.Model = (from c in DataContext.Categories where c.CategoryID == id select c).FirstOrDefault();
return View("Delete");
}
My Delete View Looks like this:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PracticeApp.Models.Category>" %>
Delete
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<fieldset>
<legend>Fields</legend>
<div class="display-label">CategoryID</div>
<div class="display-field"><%: Model.CategoryID %></div>
<div class="display-label">Category1</div>
<div class="display-field"><%: Model.Category1 %></div>
</fieldset>
<% using (Html.BeginForm()) { %>
<p>
<input type="submit" value="Delete" /> |
<%: Html.ActionLink("Back to List", "List") %>
</p>
<% } %>
Ok. This will all seem familiar to MVC Experts. Mind, I just started with MVC and I do not have Extremely good knowledge with Linq Either, so I am expecting some flaming from my following Code (Which doesn’t even run – the purpose of the question – I don’t know why it does not run!)
It is in my categoriesController
[HttpPost]
public ActionResult Delete(Models.Category category)
{
foreach (Models.Category c in DataContext.Categories)
{
if (c.CategoryID == category.CategoryID)
{
DataContext.Categories.DeleteObject(c);
}
}
DataContext.SaveChanges();
return RedirectToAction("List", "List", "Categories");
}
Any Ideas as to why my code refuses to run? I breakpointed at the first brace. Breakpoint never gets reached.
The problem is that your GET action (
DeleteCategory) doesn’t have the same as your POST action (Delete).So you need to specify the action you want to invoke:
Or if you don’t want to do that you should rename your GET method to be he same as the POST method:
Now since both your GET and POST actions are named the same way, you could use the
BeginFormhelper without arguments because the distinction between those actions will be based on the HTTP verb used to invoke them:As a side note inside your form you don’t have any input field. So don’t expect to get anything out of this Category that your POST action expects. You might need to at least include a hidden field containing the id of the category that you are willing to delete.