views:

93

answers:

1

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)?

+1  A: 

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.

BuildStarted