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())属性がデフォルトで現在の URIである HTML<form>要素をレンダリングします。action

つまり、現在 にいる場合/Dinner/Delete/5、フォームはその URI に投稿され、通常のモデル バインディングが開始され、 がルート パラメータにマッピングされ5ます{id}

于 2012-11-21T07:36:32.487 に答える