Solved

Web API Custom Bad Request Response

Posted on 2013-02-07
2
600 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connect Gridview column to Textbox in C# 2 55
asp.net, radiobuttonlist, c# 3 69
AJAX Create / Update Not working MVC 5 64
PowerShell:  foreach where object notmatch? 17 83
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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