Вопрос

I'm pretty new to MVC and I'm looking at the code of NerdDinner

View:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<NerdDinner.Models.Dinner>" %>

<asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server">
    Delete Confirmation: <%:Model.Title %>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">

    <h2>
        Delete Confirmation
    </h2>

    <div>
        <p>Please confirm you want to cancel the dinner titled: 
        <i> <%:Model.Title %>? </i> </p>
    </div>

    <% using (Html.BeginForm()) { %>

        <input name="confirmButton" type="submit" value="Delete" />        

    <% } %>

</asp:Content>

Controller:

    [HttpPost, Authorize]
    public ActionResult Delete(int id, string confirmButton) {

        Dinner dinner = dinnerRepository.GetDinner(id);

        if (dinner == null)
            return View("NotFound");

        if (!dinner.IsHostedBy(User.Identity.Name))
            return View("InvalidOwner");

        dinnerRepository.Delete(dinner);
        dinnerRepository.Save();

        return View("Deleted");
    }

How does the controller get the ID of the item to delete? It seems like nothing on the view is containing the ID to be passed to the controller.

Это было полезно?

Решение

using (Html.BeginForm()) will render an HTML <form> element whose action attribute is by default the current URI.

That means that if you are currently at /Dinner/Delete/5, the form will post to that URI and the regular model binding will come into play, mapping the 5 to the {id} route parameter.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top