I want to use custom action filter to manipulate parameters to one action.
User inputs: 2 names in a form ;
Action: actually needs to take 2 ids;
Action Filter (onExecuting, will verify the input names and if valid, convert them into 2 ids and replace in the routedata)
because i don't want to put validation logic in Action Controller.
here's part of the code:
Routing Info
routes.MapRoute( "Default", // Route name "{controller}/{action}", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults );
routes.MapRoute( "RelationshipResults", // Route Name "Relationship/{initPersonID}/{targetPersonID}", // URL with parameters new { controller = "Relationship", action = "Results" });
Form to submit (Create 2 input box and submit via jquery)
<% using (Html.BeginForm("Results", "Relationship", FormMethod.Post, new { id = "formSearch" })) {%>
...<td align="left"><%: MvcWeibookWeb.Properties.Resource.Home_InitPersonName%></td> <td align="right"> <%= Html.TextBox("initPersonID")%></td> <td rowspan="3" valign="top"> <div id="sinaIntro"> <%: MvcWeibookWeb.Properties.Resource.Home_SinaIntro %> <br /> <%: MvcWeibookWeb.Properties.Resource.Genearl_PromotionSina %> </div> </td> </tr> <tr> <td align="left" width="90px"><%: MvcWeibookWeb.Properties.Resource.Home_TargetPersonName%></td> <td align="right"><%= Html.TextBox("targetPersonID")%></td> </tr> <tr> <td colspan="2" align="right"> <a href="#" class="btn-HomeSearch" onclick="$('#formSearch').submit();"><%: MvcWeibookWeb.Properties.Resource.Home_Search%></a> </td>
Action Filter
public override void OnActionExecuting(ActionExecutingContext filterContext) { Sina.Searcher searcher = new Sina.Searcher(Sina.Processor.UserNetwork); String initPersonName, targetPersonName;
// do sth to convert it to ids and replace// form submit names, we need to process them and convert them to IDs before it enters the real controller. initPersonName = filterContext.RouteData.Values["initPersonID"] as String; targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;
Action/Controller
[ValidationActionFilter] [HandleError] public ActionResult Results( Int64 initPersonID, Int64 targetPersonID) { ...
My problem is: in the actionFilter, it never gets the 2 parameters "initPersonID" and "targetPersonID", the RouteData.Values don't contain these 2 keys...
:(