Solved

Web API Custom Bad Request Response

Posted on 2013-02-07
2
592 Views
Last Modified: 2013-02-11
I started with this question...
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_28022765.html

And I came close to the right answer, but not quite there...

    public class ErrorCollection
    {
        public string Message { get; set; }
        public Dictionary<string, string> ModelState;
    }

  var errorCollection = new ErrorCollection();
  errorCollection.Message = "The request is invalid.";
  errorCollection.ModelState = new Dictionary<string, string>() { { "pobox.Pobox_txt", "PO Box Data should be limited to 10 characters." } };
  var response = Request.CreateResponse<object>(HttpStatusCode.BadRequest, errorCollection);
  throw new HttpResponseException(response);

The JSON returned looks like this...
{"ModelState":{"pobox.Pobox_txt":"PO Box Data should be limited to 10 characters."},"Message":"The request is invalid."}

The JSON that would do what I need regarding populating the web page with validation failure messages would look like this
{"ModelState":{"pobox.Pobox_txt":["PO Box Data should be limited to 10 characters."]},"Message":"The request is invalid."}

Note the addition of the SQUARE BRACKETS in the return.

MY QUESTION
Is there a way to better define my class to get those square brackets to show up?
0
Comment
Question by:gswitz
  • 2
2 Comments
 

Author Comment

by:gswitz
ID: 38865272
The reason for my problem is that when the unobtrusive validation occurs on the page, it is only writing the first letter of the string into the field to show the error to the user, so where it should say "PO Box Data should be limited to 10 characters." it says only "P".

Thanks!!
0
 

Accepted Solution

by:
gswitz earned 0 total points
ID: 38877830
This was using jquery.validate and jquery.validate.unobtrusive.

    $.validator.addMethod("failure", function () { return false; });
    $.validator.unobtrusive.adapters.addBool("failure");
    $.validator.unobtrusive.revalidate = function (form, validationResult) {
        $.removeData(form[0], 'validator');
        var serverValidationErrors = [];
        for (var property in validationResult) {
            var elementId = property.toLowerCase();
            elementId = elementId.substr(elementId.indexOf('.') + 1);
            var item = form.find('#' + elementId);
            serverValidationErrors.push(item);
            item.attr('data-val-failure', validationResult[property]);
            jQuery.validator.unobtrusive.parseElement(item[0]);
        }

I never found the solution to my problem (specifically the brakets in the JSON return). Instead, I removed the array parameter that specified [0] for the item from the JSON to be written to the exception element.

So, my exception is being written as I want, but it wasn't the solution I preferred.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question