sixfeet
sixfeet

Reputation: 964

Modify the key/value pair of a ModelState MVC attribute

Is there any way to modify the key of an ModelState attribute? I receive the following JSON response:

{"Message":"The request is invalid.",
 "ModelState":
     { 
      "supplier.SupplierWarehouses[2].Location":["Location name is too long, maximum 50 characters"] 
     }
 }

I want to modify the supplier.SupplierWarehouses[2].Location to something more simple like SupplierLocation.

Is there any way to do this directly in the Model? My current Model is:

public class SupplierWarehouseMetadata
{
    [StringLength(50,ErrorMessage="Location name is too long, maximum 50 characters")]
    public string Location { get; set; }
}

Upvotes: 0

Views: 436

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239430

No, because this isn't just about ModelState. The post value has that name because that's what the modelbinder needs in order to bind it correctly to the appropriate property on your model on post. If you modify the name, then the value will no longer bind.

That said, what is the goal here anyways? If you're concerned about what's coming back in the JSON, just return something custom instead of dumping ModelState.

Upvotes: 1

Related Questions