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.