2012-11-12 123 views
4

我在设置路由到Orchard中的自定义控制器时遇到了一些麻烦。在Orchard中路由自定义控制器CMS

我创建了一个观点:

@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> 
} 

与控制器:

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 /内容/电子邮件/发送

,显然失败。任何人都可以向我指出我做错了什么吗?

回答

8

试试这个:

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

增加的面积就是这样确保只有被搜索匹配的控制器/ action方法对您的模块的额外条款。

+0

这样做的窍门,谢谢! – Halceyon