在这里,我尝试使用按钮onClick事件CreateException
上的AJAX在AttorneyController
的发布方法onclick="submitForm()"
上发布表单。
现在的问题是,每当我单击按钮时,它都不会碰到CreateException
的post方法AttorneyController
并抛出错误POST http://localhost:58765/Attorney/CreateException 400 (Bad Request)
。
对我的代码的任何帮助将深表感谢。谢谢
下面是我的ajax
<script>
function submitForm() {
var datastring = $("#attorneyExceptionForm").serialize();
var validForm = $("#attorneyExceptionForm").valid();
if (!validForm) { return false; }
$.ajax({
type: "POST",
url: "/Attorney/CreateException",
data: datastring,
dataType: "json",
success: function (data) {
if (data.success) {
hideModalPopup();
showNotification("Location saved successfully", "success");
}
else {
showNotification("Failed to save the location", "warning");
}
},
error: function (data) {
debugger;
showNotification("Failed to save the location", "warning");
}
});
}
以下是我的asp.net core 2.0表单
<form action="CreateException" role="form" enctype="multipart/form-data" id="attorneyExceptionForm">
@Html.HiddenFor(x => x.Id)
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Description" class="control-label"></label>
<input asp-for="Description" class="form-control" />
<span asp-validation-for="Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Entity" class="control-label"></label>
<input asp-for="Entity" class="form-control" />
<span asp-validation-for="Entity" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Exception" class="control-label"></label>
<input asp-for="Exception" class="form-control" />
<span asp-validation-for="Exception" class="text-danger"></span>
</div>
<div class="form-group">
<button type="button" onclick="submitForm()" class="btn btn-s-md btn-primary saveButton mb-10">Save</button>
</div>
</form>
以下是我的发帖方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateException(AttorneyViewModel attorneyVM)
{
try
{
if (attorneyVM != null)
{
if (attorneyVM.Status != "0")
{
//var saveExceptionsResult = _attorneyService.SaveExcetion(attorneyVM);
return Json(new { success = _attorneyService.SaveExcetion(attorneyVM) });
}
}
return Json(new { success = false });
}
catch (Exception ex)
{
return View();
}
}
下面是get方法
public ActionResult CreateException(int Id = 0)
{
AttorneyViewModel attrVm = new AttorneyViewModel();
//if (Id > 0)
//{
// attrVm = _attorneyService.GetAttorneyById(Id);
//}
attrVm.Id = Id;
List<exceptionStatus> exceptionsStatusList = new List<exceptionStatus>();
exceptionsStatusList.Add(new exceptionStatus { Id = 1, ExceptionStatusName = "Active" });
exceptionsStatusList.Add(new exceptionStatus { Id = 2, ExceptionStatusName = "Inactive" });
exceptionsStatusList.Insert(0, new exceptionStatus { Id = 0, ExceptionStatusName = "Select Status" });
ViewBag.AttorneyStatus = exceptionsStatusList;
return PartialView("_CreateException",attrVm);
}