1
votes

I have created a web api and it was working successfully before. However, something has changed (not sure what) and it is now no longer working.

I used fiddler to compose a PUT to /api/Dealer/19 with the following JSON Data:

{"notes":[],"dealerId":19,"name":"4th Street Auto Co.","phone":"888-776-5085","website":"http://www.cars.com/dealers/98681/4th-street-auto-co/","streetAddress":"5109 Preston Highway","city":"Louisville","state":"KY","zipCode":40213}

I get a 400 error response:

HTTP/1.1 400 Bad Request Server: ASP.NET Development Server/11.0.0.0 Date: Tue, 31 Dec 2013 00:07:12 GMT X-AspNet-Version: 4.0.30319 Cache-Control: no-cache Pragma: no-cache Expires: -1 Content-Type: application/json; charset=utf-8 Content-Length: 123 Connection: Close

{"Dealer.Name":"Please Enter Name","Dealer.Phone":"Please Enter Phone Number","Dealer.Website":"Please Enter Website"}

I have a ValidationActionFilter which is building the error message:

public class ValidationActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext context)
    {
        var modelState = context.ModelState;
        if (!modelState.IsValid)
        {
            var errors = new JObject();
            foreach (var key in modelState.Keys)
            {
                var state = modelState[key];
                if (state.Errors.Any())
                {
                    errors[key] = state.Errors.First().ErrorMessage;
                }
            }

            context.Response = context.Request.
                CreateResponse<JObject>(HttpStatusCode.BadRequest, errors);
        }
    }
}

My Model looks like this:

public class Dealer
{
    public int DealerId { get; set; }

    [Required(ErrorMessage = "Please Enter Name")]
    [StringLength(100, ErrorMessage = "Name is too long")]
    public string Name { get; set; }

    [Required(ErrorMessage="Please Enter Phone Number")]
    [StringLength(14, ErrorMessage = "Phone is too long")]
    public string Phone { get; set; }

    [Required(ErrorMessage = "Please Enter Website")]
    [StringLength(250, ErrorMessage = "Website is too long")]
    public string Website { get; set; }

    [StringLength(100, ErrorMessage = "Street Address is too long")]
    public string StreetAddress { get; set; }

    [StringLength(50, ErrorMessage = "City is too long")]
    public string City { get; set; }

    [StringLength(50, ErrorMessage = "State is too long")]
    public string State { get; set; }

    public int ZipCode { get; set; }

    public virtual ICollection<Note> Notes { get; set; }
}

What is causing the validation errors even though the fields are there?

Thanks!

1
Can you show the controller action method? It may be missing the [FromBody] attribute.Matt Tester

1 Answers

1
votes

I guess the MVC cannot bind the data to your model and therefore the model is null?

If this is the case, try to reformat the data you post to reflect the model

{"Dealer.Name":"..","Dealer.Phone":"..."...

Instead of {"name":...,"phone":...}