Routing a custom controller in Orchard CMS

天大地大妈咪最大 提交于 2019-12-10 14:55:51

问题


I'm having some trouble setting up the routing to a custom controller in Orchard.

I've created a 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>
}

With a 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();
        } 
    }

And setup the route:

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())
                }
            };
        }
    }

But when I click the submit button, it tries to post to

OrchardLocal/Contents/Email/Send

and obviously fails. Can anyone point me in the direction of what I'm doing wrong?


回答1:


Try this:

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

Adding the area is like an extra clause that ensures only your module is searched for a matching controller/action method pair.



来源:https://stackoverflow.com/questions/13338963/routing-a-custom-controller-in-orchard-cms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!