0

我对 MVC 很陌生,我正在查看 NerdDinner 的代码

看法:

<%@ 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>

控制器:

    [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");
    }

控制器如何获取要删除的项目的 ID?视图上似乎没有任何内容包含要传递给控制器​​的 ID。

4

1 回答 1

0

using (Html.BeginForm())将呈现一个 HTML<form>元素,其action属性默认为当前 URI。

这意味着如果您当前位于/Dinner/Delete/5,表单将发布到该 URI,并且常规模型绑定将发挥作用,将 映射5{id}路由参数。

于 2012-11-21T07:36:32.487 回答