Gri*_*zly 6 c# ajax jquery asp.net-mvc-5 asp.net-web-api2
我正在使用asp.net webapi控制器,在我的项目中,我有一个我自己构建的DLL.该dll用于验证用户正在键入的人是否确实存在.
这是我的控制器方法:
// POST: api/EventsAPI
[ResponseType(typeof(Event))]
public IHttpActionResult PostEvent(Event @event)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (@event.DateEndOfEvent < @event.DateOfEvent) // successfully returns error.modelState (in view code)
{
ModelState.AddModelError("DateEndOfEvent", "End Date Cannot Be Before Start Date!");
return BadRequest(ModelState);
}
if (!EmpData.IsValid(@event.PersonWorkedOne)) // returns error.modelState as undefined (in view code)
{
ModelState.AddModelError("PersonWorkedOne", "This person does not exist!");
return BadRequest(ModelState);
}
if (!string.IsNullOrWhiteSpace(@event.PersonWorkedTwo))
{
if (!EmpData.IsValid(@event.PersonWorkedTwo)) // returns error.modelState as undefined (in view code)
{
ModelState.AddModelError("PersonWorkedTwo", "This persondoes not exist!");
return BadRequest(ModelState);
}
}
db.Event.Add(@event);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = @event.Id }, @event);
}
Run Code Online (Sandbox Code Playgroud)
现在,上述两个条件语句有EmpData.. EmpData是我的DLL.
这是我视图中的ajax代码:
$("form").data("validator").settings.submitHandler =
function(form) {
$.ajax({
method: "POST",
url: infoGetUrl,
data: $("form").serialize(),
success: function() {
toastr.options = {
onHidden: function () {
window.location.href = newUrl;
},
timeOut: 3000
}
toastr.success("Event successfully created.");
},
error: function (jqXHR, textStatus, errorThrown) {
var status = capitalizeFirstLetter(textStatus);
var error = $.parseJSON(jqXHR.responseText);
var modelState = error.modelState;
console.log(modelState);
$.each(modelState,
function (key, value) {
var id = "";
if (key === "$id") {
id = "#" +
key.replace('$', '').substr(0, 1).toUpperCase() +
key.substr(2);
} else {
id = "#" +
key.replace('$', '').substr(0, 1).toUpperCase() +
key.substr(1);
var status = capitalizeFirstLetter(textStatus);
console.log(key);
toastr.error(status + " - " + modelState[key]);
}
var input = $(id);
console.log(id); // result is #id
if (input) { // if element exists
input.addClass('input-validation-error');
}
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
现在,在控制器中,当我有目的地测试以获得错误消息,假设结束日期在开始日期之前时,我收到了error.modelState.但是,当我有目的地测试以获取错误消息,说明一个人不存在时......我没有得到error.modelState...返回为undefined.
使用自定义DLL时返回的ModelState不起作用吗?
任何帮助表示赞赏.
我能够在ADyson的帮助下解决这个问题.我编辑了我的DLL文件,只返回一个bool对象.
最初如果我正在检查的IsValid是除了true以外的任何东西,那么我会抛出导致此错误的异常.
因此,将异常部分取出并返回true或false.
原版的
public static bool IsValid(string person)
{
bool empExists = lstAllEmps.Any(x => x.IDNumber == person);
if (empExists)
{
return empExists;
}
else
{
var exceptionMessage = string.Format("The person, {0}, does not exist!", person);
throw new ArgumentException(exceptionMessage, person);
}
}
Run Code Online (Sandbox Code Playgroud)
新
public static bool IsValid(string person)
{
bool empExists = lstAllEmps.Any(x => x.IDNum == person);
return empExists;
}
Run Code Online (Sandbox Code Playgroud)