NerdDinner的ActionResult删除中的“confirmButton”是什么?

发布于 2024-09-13 04:08:10 字数 377 浏览 9 评论 0原文

在 NerdDinner MVC 应用演示中,在设置 ActionResult Delete 时定义了 confirmButton

public ActionResult Delete(int id, string confirmButton) {

confirmButton 的用途是什么,因为它没有在代码?我假设它返回了单击的提交按钮的名称,但它只是一个空白字符串。如何获取按下的按钮(例如,您可以有 archivedelete (或 yesno )按钮在同一页面上)?

In the NerdDinner MVC app demo, there is confirmButton defined when setting up ActionResult Delete:

public ActionResult Delete(int id, string confirmButton) {

What is confirmButton for, as it is not used in the code? I assumed it returned the name of the submit button that was clicked, but it is just a blank string. How can you get which button was pressed (e.g. you could have archive and a delete (or yes , no) buttons on the same page)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2024-09-20 04:08:10

如果您查看视图 Delete.aspx,您将看到以下 html...

<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" />        

<% } %>

如您所见,confirmButton 位于此处,并且该值将传递给您指定的 ActionResult。

您还可以指定两个按钮,例如...

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

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

<% } %>

confirmButton 参数将具有您单击的任何一个按钮的值...

为什么它在 NerdDinner 中不能正常工作很奇怪,但您可以轻松地通过创建一个快速项目并打开默认的 HomeController 并添加来测试这一点

    [HttpPost]
    public ActionResult Index(string confirmButton) {
        return View();
    }

。在 Index.aspx 中您可以添加

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

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

<% } %>

,您应该可以开始了。

If you look at the View Delete.aspx you'll see the following html...

<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" />        

<% } %>

As you can see the confirmButton is located here and the value will be passed to the ActionResult you specified.

You can also specify two buttons like...

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

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

<% } %>

The confirmButton parameter will have the value of whichever one you clicked...

Why it isn't working properly for you in NerdDinner is strange though but you can easily test this by creating a quick project and opening the default HomeController and adding

    [HttpPost]
    public ActionResult Index(string confirmButton) {
        return View();
    }

In the Index.aspx you can add

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

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

<% } %>

And you should be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文