Asp.NET MVC ActionFilter cannot get Form Submit data

Posted by Goden on Stack Overflow See other posts from Stack Overflow or by Goden
Published on 2010-05-18T07:10:38Z Indexed on 2010/05/18 7:20 UTC
Read the original article Hit count: 679

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:

  1. 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" });

  2. 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>
    
  3. 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
  4. 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...

:(

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about action-filter