asp.net-mvc-3 - 在 Orchard CMS 中路由自定义 Controller

标签 asp.net-mvc-3 asp.net-mvc-routing orchardcms

我在设置到 Orchard 中的自定义 Controller 的路由时遇到了一些问题。

我创建了一个 View :

@model dynamic
@{
    Script.Require("jQuery");
}
@using (Html.BeginForm("Send", "Email", FormMethod.Post, new { id = "contactUsForm" }))
{
    <fieldset>
        <legend>Contact Us</legend>
        <div class="editor-label">Name:</div>
        <div class="editor-field">
            @Html.TextBox("Name", "", new {style = "width: 200px"})
        </div>
        <div class="editor-label">Email Address:</div>
        <div class="editor-field">
            @Html.TextBox("Email", "", new {style = "width: 200px"})
        </div>
        <div class="editor-label">Telephone Number:</div>
        <div class="editor-field">
            @Html.TextBox("Telephone", "", new {style = "width: 200px"})
        </div>
        <div class="editor-label">Message:</div>
        <div class="editor-field">
            @Html.TextArea("Message", "", new {style = "width: 200px"})
        </div>
        <br/>
        <input id="ContactUsSend" type="button" value="Submit" />
    </fieldset>
}
@using (Script.Foot()) {
    <script>
        $(function() {
            $('#ContactUsSend').click(function () {
                alert('@Url.Action("Send", "Email")');
                var formData = $("#contactUsForm").serializeArray();

                $.ajax({
                    type: "POST",
                    url: '@Url.Action("Send", "Email")',
                    data: formData,
                    dataType: "json",
                    success: function (data) {
                        alert(data);
                    }
                });
            });
        });
    </script>
}

使用 Controller :
public class EmailController : Controller
    {
        [HttpPost]
        public ActionResult Send()
        {
            var orchardServices = DependencyResolver.Current.GetService<IOrchardServices>();
            var messageHandler = DependencyResolver.Current.GetService<IMessageManager>();
            var svc = new ContactUsService(orchardServices, messageHandler);
            svc.DoSomething();
            return new EmptyResult();
        } 
    }

并设置路线:
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 = 15,
                    Route = new Route(
                        "ContactUsWidget",
                        new RouteValueDictionary {
                            {"area", "ContactUsWidget"},
                            {"controller", "Email"},
                            {"action", "Send"}
                        },
                        new RouteValueDictionary(),
                        new RouteValueDictionary {
                            {"area", "ContactUsWidget"}
                        },
                        new MvcRouteHandler())
                }
            };
        }
    }

但是当我点击提交按钮时,它会尝试发布到

OrchardLocal/Contents/Email/Send



显然失败了。谁能指出我做错的方向?

最佳答案

试试这个:

@using (Html.BeginForm("Send", "Email", new { area = "Your.Module" }, FormMethod.Post, new { id = "contactUsForm" }))

添加区域就像一个额外的子句,确保只有你的模块被搜索到匹配的 Controller / Action 方法对。

关于asp.net-mvc-3 - 在 Orchard CMS 中路由自定义 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13338963/

相关文章:

ASP.NET Core 更改 AccessDenied 路由

c# - MVC 6 路由,SPA 回退 + 404 错误页面

localization - Orchard 多站点定位

visual-studio-2010 - 由于 obj 文件夹中的 web.config,MVC3 项目构建失败

c# - 如何在单个 mvc3 View 中添加两个表?

jquery - 级联下拉菜单 MVC 3

C# Chart Control 为大高度生成巨大的标题

c# - 如何从保存的数据在 ASP.NET MVC 中重建 URL/路由?

menu - Orchard CMS 1.5 导航子菜单

asp.net-mvc - 部分或组名称 'system.web.webPages.razor' 已定义。对此的更新可能只发生在定义它的配置级别