You should add get and set for property:
public int DataPeriodId{get;set}
You added write just
public int DataPeriodId
And Mvc can not bind.
Question
I want to bind this json:
{
"DataPeriodId": 1,
"MarketerId": 2,
"MarketerTitle": "mehrdad",
"CreditValues": [
{
"Title": "twelve",
"CreditTypeId": 1,
"Value": 2500000,
"CreditId": 3,
"HasMax": true
},
{
"Title": "test",
"CreditTypeId": 2,
"Value": "",
"CreditId": "",
"HasMax": true
},
{
"Title": "pouya",
"CreditTypeId": 3,
"Value": 1564564,
"CreditId": 7,
"HasMax": false
},
{
"Title": "jafar",
"CreditTypeId": 4,
"Value": 0,
"CreditId": 9,
"HasMax": false
},
{
"Title": "saeed",
"CreditTypeId": 5,
"Value": 0,
"CreditId": 10,
"HasMax": false
}
]
}
To this object:
public class MarketerCredit
{
public int DataPeriodId;
public int MarketerId;
public string MarketerTitle;
public List<CreditValue> CreditValues;
}
public class CreditValue
{
public int CreditId;
public int CreditTypeId;
public decimal? Value;
public bool HasMax;
}
in mvc.net.
But when pass the json to action all member is null. This is my action:
public void EditMarketerCredit(MarketerCredit marketerCredit)
{
}
So i write this:
public void EditMarketerCredit(List<CreditValue> CreditValues,int DataPeriodId,int MarketerId,string MarketerTitle)
When separate the members all thing is ok. How can i bind the json to model?
Solution 3
You should add get and set for property:
public int DataPeriodId{get;set}
You added write just
public int DataPeriodId
And Mvc can not bind.
OTHER TIPS
According to your json, you need a root object encapsulating MarketerCredit
public class RootObject
{
public MarketerCredit MarketerCredit { set; get; }
}
BTW: your json contains a line "CreditId": "",
, So type of CreditId
should be string
You will need to implement your own custom model binder and then register it when the application starts. Here's a walkthrough explaining how to achieve this.
But, if you want to avoid customizing the MVC Pipeline you will need to create a new class/model that exposes a property called marketerCredit
of type MarketerCredit
, you can name this model whatever you like
public class MyModel
{
public MarketerCredit marketerCredit{get;set;}
}
then modify your action method like this...
public void EditMarketerCredit(MyModel myModel)
{
}