Asp.NET MVC ActionFilter cannot get Form Submit data
- by Goden
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("initPersonName")%></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("targetPersonName")%></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;
// form submit names, we need to process them and convert them to IDs before it enters the real controller.
initPersonName = filterContext.RouteData.Values["initPersonName"] as String;
targetPersonName = filterContext.RouteData.Values["targetPersonName"] as String;
// do sth to convert it to ids and replace
Action/Controller
[ValidationActionFilter]
[HandleError]
public ActionResult Results( Int64 initPersonName, Int64 targetPersonName)
{ ...
My problem is: in the actionFilter, it never gets the 2 parameter "initPersonName" and "targetPersonName", the RouteData.Values don't contain these 2 keys...
:(