我正在嘗試創建一個表單,爲創建新用戶創建一個Ajax帖子。我的目標是顯示一個彈出窗口,指出操作的結果。當前版本的動作方法會在模型狀態出錯時向模型狀態添加錯誤,如果成功則重定向。 AdminController.cs內如何使用Ajax調用此後期操作方法?
操作方法:
[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>
從上面可以看出,與 「usercreatebutton」 按鈕的id是我發展的按鈕,我希望把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()'的文檔嗎? HTTP://api.jquery。com/category/ajax/ –
@JonathanM是的,我有和多次嘗試沒有奏效。 – ITWorker
請向我們展示您使用'$ .ajax()'進行的最新嘗試,我相信我們可以幫助您實現它。 –