c# - Json object with numeric member is not binding to Object.IntegerProperty in MVC controller -
using jquery 1.9, i'm calling following ajax statement:
var data = { propertyid: 1912, position: 0, description: "state sales tax", rate: 5, rateispercent: true }; $.ajax( { type : "put", url: "/api/taxes/5", data: json.stringify(data), contenttype: "application/json; charset=utf-8" });
in chrome or firefox, see request body (the stringified data) valid json. looks this:
i've verified other parsers, of confirm valid json.
the problem propertyid member in json object not bind object property in mvc controller method, though other members do. error of:
{ "message": "the request invalid.", "modelstate": { "newtax.propertyid": [ "the field propertyid must between 1 , 2147483647." ] } }
as can see in above image, propertyid value 1912. acceptable int32 value.
so why throwing validation error?
here's object i'm wanting bound to:
public class tax : cloneableobject { public tax() { } public int id { get; set; } [required] [range(1, int.maxvalue)] [ignoredatamember] [display(name = "property id")] public int propertyid { get; set; } [required] [min(0)] public int position { get; set; } [required] [stringlength(256)] public string description { get; set; } [required] [datatype(datatype.currency)] public decimal rate { get; set; } [required] [display(name = "rate percent")] public bool rateispercent { get; set; } }
and here's action method in controller:
// put: api/taxes/5 public tax put(int id, [frombody]tax newtax) { return taxbusiness.update(id, newtax).executeorthrow(); }
here routes configured:
config.routes.maphttproute(name: "apiputpatchdelete", routetemplate: "api/{controller}/{id}", defaults: null, constraints: new { httpmethod = new httpmethodconstraint("put", "patch", "delete") }); config.routes.maphttproute(name: "apipost", routetemplate: "api/{controller}", defaults: null, constraints: new { httpmethod = new httpmethodconstraint("post") }); config.routes.maphttproute(name: "apiget", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional }, constraints: new { httpmethod = new httpmethodconstraint("get") });
i don't believe there's problem hitting right route because works if pass name/value post data instead of json. instance, if call instead:
curl -x put http://app/api/taxes/5 -d "propertyid=1912&rate=50&etc..."
it works fine.
the propertyid member isn't getting bound correctly. there no other validation errors other one.
what i've tried...
if remove [range(1, int.maxvalue)]
attribute, don't validation errors tax.propertyid ends value of 0
.
if remove all attributes propertyid, value comes through fine 1912
.
again, none of happens if use name/value post string send values like:
"propertyid=1912&rate=23&etc...."
all validation works intended if that. happening when post json string.
update:
it appears related [ignoredatamember]
attribute. if remove that, works. if leave that, problems occur.
is [ignoredatamember]
skipping on json object members when deserializing? thought [ignoredatamember]
used when serializing, not deserializing.
if so, there different way of telling serialization ignore propertyid field while making sure gets included during deserializing?
from i've read, there no way use attribute stop serialization , not deserialization. ignoredatamember
, jsonignore
both same thing.
the way got work remove attribute , change on shouldserialize
method conditional serialization.
Comments
Post a Comment