How do I perform a secondary action (i.e. calculate fields) in ASP.NET MVC?

爷,独闯天下 提交于 2019-11-29 12:03:24

[Saw your comments; I'll repost this answer here so you can mark the question resolved, and mark it community wiki so I don't get rep for it - Dylan]

Give your submit buttons a name, and then inspect the submitted value in your controller method:

<% Html.BeginForm("MyAction", "MyController", FormMethod.Post); %>
<input type="submit" name="submitButton" value="Send" />
<input type="submit" name="submitButton" value="Cancel" />
<% Html.EndForm(); %>

posting to

public class MyController : Controller {
    public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Send":
                // delegate sending to another controller action
                return(Send());
            case "Cancel":
                // call another action to perform the cancellation
                return(Cancel());
            default:
                // If they've submitted the form without a submitButton, 
                // just return the view again.
                return(View());
        }
    }

    private ActionResult Cancel() {
        // process the cancellation request here.
        return(View("Cancelled"));
    }

    private ActionResult Send() {
        // perform the actual send operation here.
        return(View("SendConfirmed"));
    }

}

An action link just links to an action. It translates to a <a href="action">action</a> tag. The action it links to has no idea about the state of the page it has just left.

You should probably be 'POST'ing to an action, but it's only going to send form data, not an object (although mvc can automatically map Form fields to an object).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!