【发布时间】:2016-07-25 15:26:08
【问题描述】:
我正在尝试创建一个执行 Ajax 发布以创建新用户的表单。我的目标是显示一个指示操作结果的弹出窗口。如果出现问题,当前版本的 action 方法会向 ModelState 添加错误,如果成功则重定向。
AdminController.cs 中的操作方法:
[HttpPost]
public async Task<ActionResult> Create(CreateModel model)
{
if (ModelState.IsValid)
{
AppUser user = new AppUser { UserName = model.Name, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user,
model.Password);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
else
{
AddErrorsFromResult(result);
}
}
return View(model);
}
观点:
@model IdentityDevelopment.Models.CreateModel
@{ ViewBag.Title = "Create User";}
<h2>Create User</h2>
@Html.ValidationSummary(false)
@using (Html.BeginForm("Create", "Admin", new { returnUrl = Request.Url.AbsoluteUri }))
{
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Email</label>
@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Password</label>
@Html.PasswordFor(x => x.Password, new { @class = "form-control" })
</div>
<button type="submit" onclick="return showDiv();" class="btn btn-primary">Create</button>
@Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" })
}
<button id="usercreatebutton">Create</button>
如上所见,id为“usercreatebutton”的按钮是我的开发按钮,我想把ajax函数放进去:
$("#usercreatebutton")
.button()
.click(function (event) {
alert("ajax post call");
});
另一个创建按钮用于常规表单提交。
CreateModel:
public class CreateModel
{
[Required]
public string Name { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
}
根据 Shyju 的回复,我得到了这个工作。下面我将发布对我的代码的更新:
在视图中,我修改了 BeginForm 声明以赋予表单一个 id 并将提交按钮移到其中:
@model IdentityDevelopment.Models.CreateModel
@{ ViewBag.Title = "Create User";}
<h2>Create User</h2>
@Html.ValidationSummary(false)
@using (Html.BeginForm("Create", "Admin", new { returnUrl = Request.Url.AbsoluteUri }, FormMethod.Post, new { @id = "signupform" }))
{
<div class="form-group">
<label>Name</label>
@Html.TextBoxFor(x => x.Name, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Email</label>
@Html.TextBoxFor(x => x.Email, new { @class = "form-control" })
</div>
<div class="form-group">
<label>Password</label>
@Html.PasswordFor(x => x.Password, new { @class = "form-control" })
</div>
<!-- <button type="submit" onclick="return showDiv();" class="btn btn-primary">Create</button> -->
<button type="submit" id="usercreatebutton">Create</button>
@Html.ActionLink("Cancel", "Index", null, new { @class = "btn btn-default" })
}
控制器代码被修改为来自 Shyju 的响应。
最后,javascript代码是:
$("form#signupform").submit(function (event) {
event.preventDefault();
var form = $(this);
$.post(form.attr("action"), form.serialize(), function (res) {
if (res.status === "success") {
alert(res.message);
}
else {
alert(res.message);
}
});
});
【问题讨论】:
-
您查看过 jquery
$.ajax()的文档吗? api.jquery.com/category/ajax -
@JonathanM 是的,我有,但多次尝试都没有奏效。
-
请向我们展示您对
$.ajax()的最新尝试,我相信我们可以帮助您实现它。 -
@JonathanM 我已经用有效的答案更新了我的回复。
标签: javascript jquery ajax asp.net-mvc post