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

后端 未结 2 732
囚心锁ツ
囚心锁ツ 2020-12-20 01:42

I need to do some calculations on an ASP.NET MVC View, an action different than the form submission. I\'ve tried various methods of passing the current Model on to a new con

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 02:19

    [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); %>
    
    
    <% 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"));
        }
    
    }
    

提交回复
热议问题