简体   繁体   中英

MVC.NET C#: Getting the emitted null values of a JSON http request?

I have a WebService, that expects a list of a defined type via Json.

Class:

public class myType
{
    public int id { get; set;}
    public int? varA { get; set;}
    public string varB { get; set;}
    public float? varC { get; set;}
}

Logic:

//Passed JSON --> Data:{'id':'1','varA':''}
[HttpPost]
public JsonResult Update(myType Data)
{
    //Data ends up with value's id = 1; varA = null; varB = null; varC = null;
    //So how can I find that valueA was actually passed with a null value, and all the 
    //others are just nulling by default?
    if (Data.varA.HasValue) dbItem.varA = Data.varA
    ...
}

Lets assume myType has several values, eg {id, varA, varB, varC} all values except id are nullable.

Now, I purposely want to null out only 1 value

the problem is, For bandwidth conservation, I'm only sending the id, and whatever value has been updated in JSON eg: Data:{'id':'1','valueA':''} , as far as the method is consurned though, the Data Object, returns the object that gives the id , but also gives a null value for all other variables of the object.

So, what can I to to be able to tell which emited variables were sent via the json to the webmethod? So I can tell which variable I was really wanting to null?

More info: Using ExtJS 4 For the front End. This question is in relation to Ext.Store's

MVC attempts to parse the values you send to it based on the name (via normal deserialization). Therefore, if you don't specify the value it will send null as expected because you have nullable properties. Why don't you pass actual values in valueA, -1 for example. If you don't specify values for nullable properties they will be null...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM