开发者

What is `confirmButton` in NerdDinner's ActionResult Delete?

开发者 https://www.devze.com 2023-01-10 15:57 出处:网络
In the NerdDinner MVC app demo, there 开发者_StackOverflow中文版is confirmButton defined when setting up ActionResult Delete:

In the NerdDinner MVC app demo, there 开发者_StackOverflow中文版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)?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消