这有点奇怪,因为几周前这个工作正常。这就是我的......
我有一个部分视图_subscribe具有ajax表单
@model m60_mbt.Models.Subscriber
@{bool? subscribed = (bool?)Session["subscribed"];}
@if (subscribed == true)
{
<div class="subscribe text-center animated fadeInUp">
<h1>
Thank you for subscribing to our newsletter.
</h1>
</div>
}
else
{
<div class="row">
<h1><i class="fa fa-paper-plane"></i><span>Subscribe to stay in the loop</span></h1>
@using (Ajax.BeginForm("Create", "Subscriber", new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "subscribe_target"
}, new { id = "Subscribe" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.EmailAddress, new { @placeholder = "Your email address" })
<input id="sub_but" type="submit" value="Subscribe" />
<br />
@Html.ValidationMessageFor(model => model.EmailAddress)
}
</div>
}
这是放在视图中的。
<section class="subscribe text-center" id="subscribe_target">
<h1><i class="fa fa-paper-plane"></i><span>Subscribe to stay in the loop</span></h1>
<div id="subscribe_form">
@Html.Partial("_subscribe", new m60_mbt.Models.Subscriber());
</div>
</section>
但是,当呈现页面时,表单操作将更改为:
<form action="/Home/Create?Length=10" data-ajax="true" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#subscribe_target" id="Subscribe" method="post">
有什么想法吗?
/ *编辑* / 这是我的RouteConfig
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
答案 0 :(得分:4)
您最近添加了html属性new { id = "Subscribe" }
。由于此更改,控制器被视为路由值。要使其工作,您必须在控制器名称和ajax设置之间为路由值添加一个参数。所以,如果我将路由值指定为null,那么您的代码看起来像这样。
@using (Ajax.BeginForm("Create", "Subscriber", null, new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "subscribe_target"
}, new { id = "Subscribe" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.EmailAddress, new { @placeholder = "Your email address" })
<input id="sub_but" type="submit" value="Subscribe" />
<br />
@Html.ValidationMessageFor(model => model.EmailAddress)
}