我正在为我的mvc应用程序创建角色。当我逐步完成代码时,一切正常。当我不运行调试工具时,我会收到警报(如果我试图询问xHP.responseMessage,则不确定)。有什么想法吗?
这个答案看起来可能适用,但成功是一个回调,所以我不确定它会如何应用
javascripts works when I step through doesnt if I dont
@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
<div class="row">
<form id="frmRoleEditor" class="form-inline">
<div class="form-group">
@Html.LabelFor(m => m.Name, new { @class = "col-sm-3" })
<div class="col-sm-9">
@Html.TextBoxFor(m => m.Name, new { @class = "form-control", required = "true", minlength = "2", maxlength = "256" })
</div>
</div>
<div class="form-group">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" value="Update" />
</div>
</form>
</div>
<script type="text/javascript">
//$(function () {
// $("#frmRoleEditor").validate();
//});
</script>
[HttpPost]
public JsonResult RoleEditor(IdentityRole model)
{
try
{
using (ApplicationDbContext context = new ApplicationDbContext())
{
var exists = context.Roles.SingleOrDefault(r => r.Name == model.Name);
if (exists == null)
{
context.Roles.Add(model);
context.SaveChanges();
}
}
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Roles", "Admin");
return Json(new { success = true, Url = redirectUrl });
}
catch (Exception ex)
{
throw;
}
}
$(function () {
$("#btnNewRole").on("click", function () {
$("#modalRole").modal("show");
});
$("body").on("submit", "#frmRoleEditor", function () {
var name = $("#Name").val();
var model = {
Name: name
};
$.ajax({
url: "/Admin/RoleEditor",
type: "POST",
data: model,
success: function (data) {
if (data.success) {
window.location.href = data.Url;
}
},
error: function (xHP, status, error) {
alert('wtf');
}
});
});
});
更新