gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:19:56 25 4
gpt4 key购买 nike

我在设置到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com