ajax actionlink看起来像这样:
@Ajax.ActionLink("Create account", "RegisterNewUser", "RegisteredUsers", new AjaxOptions { UpdateTargetId = "updaterDiv", HttpMethod = "POST" }, new { @class = "actionButtons" }
当RegisterNewUser Action看起来像这样:
public ActionResult RegisterNewUser(string uName, string email, string password)
{
ViewBag.Message = "Register";
string msg = "";
if (!users.RegisterUser(uName, email, password))
{
msg = "The provided email already exists";
}
ViewBag.DeniedMsg = msg;
return PartialView("_Register");
}
ajax actionlink工作正常。但是当我向动作方法添加[HttpPost]
时(顺便说一句,由ajax actionlink调用),链接停止响应。点击它没有任何反应。我究竟做错了什么?包含链接的表单如下所示:
@using (Html.BeginForm(new { @class = "registerForm" }))
{
@Html.LabelFor(model => model.nickName)
@Html.TextBoxFor(model => model.nickName, new { @class = "RegisterControls" })
<br /><br />
@Html.LabelFor(model => model.email)
@Html.TextBoxFor(model => model.email, new { @class = "RegisterControls" })
@Html.ValidationMessageFor(model => model.email)
<br /><br />
@Html.LabelFor(model => model.password)
@Html.PasswordFor(model => model.password, new { @class = "RegisterControls", id = "firstPassword" })
<br /><br />
@Html.LabelFor(model => model.confirmPassword)
@Html.PasswordFor(model => model.password, new { @class = "RegisterControls" })
@Html.ValidationMessageFor(model => model.confirmPassword)
<br /><br /><br />
<hr style="color: #D5E0EE; border: solid; border-style: double;" />
@Ajax.ActionLink("Create account", "RegisterNewUser", "RegisteredUsers", new AjaxOptions { UpdateTargetId = "updaterDiv", HttpMethod = "POST" }, new { @class = "actionButtons" })
<br /><br />
<p style="color: red">@ViewBag.DeniedMsg</p>
}
我可能做错了什么?
答案 0 :(得分:1)
如果您打算使用Ajax扩展发布表单数据,则需要更改表单以使用Ajax.BeginForm
并使用提交按钮替换该按钮:
@using (Ajax.BeginForm("RegisterNewUser", "RegisteredUsers", new AjaxOptions { UpdateTargetId = "updaterDiv" }))
{
....form data
<input type="submit" value="Create account" />
}
另外,请确保在表单中加入updaterDiv
。
您需要将[HttpPost]
添加到您的控制器操作中,我也会将其更改为将您原始视图中的模型作为已发布参数。