【问题标题】:Post form to controller in MVC(Orchard)将表单发布到 MVC(果园)中的控制器
【发布时间】:2015-02-20 09:14:41
【问题描述】:

我有一个简单的表格:

<form actions="/module_name/controller_name/function_name" action="GET">
<input type="email" name="email" />
<input type="submit" value="send" />
</form>

我有我的控制器:

[Themed]
    public class controller_name: Controller
    {
        [HttpGet]
        [AlwaysAccessible]
        public ActionResult function_name()
        {
            int b;
            return new EmptyResult();
        } 
    }

数据仅用于测试。因为由于未知原因,帖子仅将我重定向到页面:localhost/module_name/controller_name/function_name?email=...

而不是进入控制器。

我做错了什么?

【问题讨论】:

  • 你为你的模块控制器设置路由了吗?
  • 关于这个问题的几点建议:您几乎不应该使用 GET 作为表单的操作,这太危险了,严重限制了表单的大小,而且没有任何意义。其次,永远不要对动作的 URL 进行硬编码。请改用 URL 帮助程序。这样,如果您的操作的 URL 因任何原因发生更改,您的代码仍然可以工作。

标签: asp.net asp.net-mvc controller orchardcms orchardcms-1.8


【解决方案1】:

您可能还没有为您的模块设置路由。创建一个如下所示的类:

public class Routes : IRouteProvider {
    public void GetRoutes(ICollection<RouteDescriptor> routes) {
        foreach (var routeDescriptor in GetRoutes())
            routes.Add(routeDescriptor);
    }

    public IEnumerable<RouteDescriptor> GetRoutes() {
        return new[] {
            new RouteDescriptor {
                Priority = 5,
                Route = new Route(
                    "Test", // this is the name of the page url
                    new RouteValueDictionary {
                        {"area", "my_module"}, // this is the name of your module
                        {"controller", "controller_name"},
                        {"action", "function_name"}
                    },
                    new RouteValueDictionary(),
                    new RouteValueDictionary {
                        {"area", "my_module"} // this is the name of your module
                    },
                    new MvcRouteHandler())
            }
        };
    }
}

【讨论】:

    猜你喜欢
    • 2013-11-10
    • 2012-09-08
    • 1970-01-01
    • 2015-01-31
    • 2017-01-01
    • 2017-06-22
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多