Asp.NET MVC ActionFilter无法获取表单提交数据

发布于 2024-09-01 18:34:12 字数 2218 浏览 1 评论 0 原文

我想使用自定义操作过滤器来操作一个操作的参数。

用户输入:表格中的 2 个姓名;

Action:实际上需要取2个id;

Action Filter(onExecuting,将验证输入名称,如果有效,将它们转换为 2 个 id 并替换为路由数据),

因为我不想将验证逻辑放入 Action Controller 中。

这是代码的一部分:

  1. 路由信息

    routes.MapRoute(
         "默认", // 路由名称
         "{controller}/{action}", // 带参数的 URL
         new {controller = "Home", action = "Index"} // 参数默认值
    );
    
    路线.MapRoute(
         "RelationshipResults", // 路由名称
         "Relationship/{initPersonID}/{targetPersonID}", // 带参数的 URL
         新{控制器=“关系”,操作=“结果”}
    );
    
  2. 要提交的表单(创建2个输入框并通过jquery提交)

    ...
     <%: MvcWeibookWeb.Properties.Resource.Home_InitPersonName%>;
          <%= Html.TextBox("initPersonID")%>
         
             
    <%: MvcWeibookWeb.Properties.Resource.Home_SinaIntro %>
    > <%: MvcWeibookWeb.Properties.Resource.Genearl_PromotionSina %>
    <%:MvcWeibookWeb.Properties.Resource.Home_TargetPersonName%> <%= Html.TextBox("targetPersonID")%> <%: MvcWeibookWeb.Properties.Resource.Home_Search%>
  3. 操作过滤器

     public override void OnActionExecuting(ActionExecutingContext filterContext)
     {
         Sina.Searcher 搜索器 = new Sina.Searcher(Sina.Processor.UserNetwork);
         字符串 initPersonName, targetPersonName;
    
         // 表单提交名称,我们需要在进入真正的控制器之前对其进行处理并将其转换为ID。
         initPersonName = filterContext.RouteData.Values["initPersonID"] as String;
         targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;
     // 做一些事情将其转换为 ids 并替换
    
  4. 动作/控制器

    <前><代码> [ValidationActionFilter] [处理错误] 公共 ActionResult 结果(Int64 initPersonID,Int64 targetPersonID) { ...

我的问题是:在actionFilter中,它永远不会获取2个参数“initPersonID”和“targetPersonID”,RouteData.Values不包含这2个键。

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 default
    );
    
    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)

    ...

         <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>
    
  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["initPersonID"] as String;
         targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;
     // do sth to convert it to ids and replace
    
  4. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的巷 2024-09-08 18:34:12

问题是,由于您的路线没有任何 initPersonNametargetPersonName 值,因此它们永远不会出现在您的路线数据中。尝试(尽管看起来有点奇怪):

initPersonName = filterContext.RouteData.Values["initPersonID"] as String;
targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;

由于“...ID”是您的路线中调用的值,因此您必须在路线数据中查找这些值。事实上,你实际上没有在 url 中包含 ID 是另一回事......

The problem is that since your routes don't have any values for initPersonName and targetPersonName, they never end up in your route data. Try (even though it looks a little odd):

initPersonName = filterContext.RouteData.Values["initPersonID"] as String;
targetPersonName = filterContext.RouteData.Values["targetPersonID"] as String;

Since "...ID" whas what the values where called in your routes, that's what you have to look for in your route data. The fact that you're actually not including the ID's in the url is another matter...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文