I am using an asp.net webapi controller, and in my project I have a dll that I built myself. The dll is being used to validate if the person that the user is typing in actually exists.
Here is my controller method:
// 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); } Now the two conditional statements above that have EmpData.. EmpData is from my dll.
Here is the ajax code in my view:
$("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');                         }                     });             }         });     } Now, in the controller when I purposefully test to get the error message concering the end date being before the start date, I receive error.modelState.  But when I purposefully test to get the error message saying that a person does not exist... I do not get error.modelState.. that returns as undefined.
Does returning ModelState not work when using custom DLL?
Any help is appreciated.
1 Answers
Answers 1
I was able to figure this out with the help of ADyson. I edited my DLL file to return just a bool object.
Originally if what I was checking in IsValid was anything other than true, then I would throw an exception which was causing this error.
So taking the exception part out and just returning either true or false worked.
Original
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);     } } New
public static bool IsValid(string person) {     bool empExists = lstAllEmps.Any(x => x.IDNum == person);       return empExists; }  
0 comments:
Post a Comment