How to post the same form to a different action in ASP.

Source: Internet
Author: User
Tags button type


Problem description


The page usually has a search and an export, and the two action requires a form that is usually the same, so you need to post the same form dynamically to a different action.


Scenario One: Implement the attribute in the MVC framework, complete the action selection logic, and set the action's information in the name of the button in the view


Add a attribute


[AttributeUsage (AttributeTargets.Method)]    public class Multiplebuttonattribute:actionnameselectorattribute    {public        string Name {get; set;}        public string Argument {get; set;}        public override bool IsValidName (ControllerContext controllercontext, String actionname, MethodInfo MethodInfo)        { C6/>var isvalidname = false;            var keyValue = string. Format ("{0}:{1}", Name, Argument);            var value = ControllerContext.Controller.ValueProvider.GetValue (keyValue);            if (value = null)            {                Controllercontext.controller.controllercontext.routedata.values[name] = Argument;                IsValidName = true;            }            return isvalidname;        }    }




Description: This attribute inherits from Actionnameselectorattribute, so when the MVC framework chooses action, the IsValidName method is called. This allows you to determine which action to invoke by judging the special parameters passed in by the front end, thus completing the mapping logic for the front view and action


Then set in view:


Search:


<div class= "Col-xs-8" >            <button type= "Submit" Name= "Action:searchdeliveries" class= "btn btn-success" >Search</button>        </div>




Export:


<button type= "Submit" Name= "Action:exporttocsv" class= "btn Btn-default" >Export</button>


In action:


[Multiplebutton (Name = "action", Argument = "Exporttocsv")]        Public ActionResult Exporttocsv () {//}[multiplebutton (Name = "action", Argument = "searchdeliveries")] public        ActionResult searchdeliveries (FormCollection FC) {//}




Scenario Two is done in javascript:


Thinking, you can redirect the action of the form when you post:




$ ("#btnDateRangeSearch"). Click (function () {                    var frm = $ ("#searchContainer"). Parent ();                    Frm.attr ("Action", "@Url. Action (" Searchdeliveries ")");                    Frm.submit ();                });                $ ("#btnExport"). Click (function () {                    var frm = $ ("#searchContainer"). Parent ();                    Frm.attr ("Action", "@Url. Action (" Exporttocsv ")");                    Frm.submit ();                });







How to post the same form to a different action in ASP.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.