Reputation:
I have the following classes in ASP.NET MVC using C# derived from a Json Model using VS menu Edit > Paste Special > Paste Json as Classes. I want to apply my own data. How can a create my data model, instead of using Rootobject obj = new Rootobject { properties here....}, so i can serialize it into a View Controller? Is there any better aprroach of doing it with EF, for example ?
public class Rootobject
{
public string MerchantOrderId { get; set; }
public Customer Customer { get; set; }
public Payment Payment { get; set; }
}
public class Customer
{
public string Name { get; set; }
}
public class Payment
{
public string Type { get; set; }
public bool Authenticate { get; set; }
public int Amount { get; set; }
public string ReturnUrl { get; set; }
public Debitcard DebitCard { get; set; }
}
public class Debitcard
{
public string CardNumber { get; set; }
public string Holder { get; set; }
public string ExpirationDate { get; set; }
public string SecurityCode { get; set; }
public string Brand { get; set; }
}
I want to serialize it into Json as the same output below but with my own data from the generated classes above:
{
"MerchantOrderId":"2014121201",
"Customer":{
"Name":"Comprador Cartão de débito"
},
"Payment":{
"Type":"DebitCard",
"Authenticate": true,
"Amount":15700,
"ReturnUrl":"http://www.cielo.com.br",
"DebitCard":{
"CardNumber":"4551870000000183",
"Holder":"Teste Holder",
"ExpirationDate":"12/2030",
"SecurityCode":"123",
"Brand":"Visa"
}
}
}
Into a View Controller using the expression:
return Json(MyModel, JsonRequestBehavior.AllowGet);
any help would be greatly appreciated!
Upvotes: 1
Views: 608
Reputation: 1119
try this:
1. in controller
public ActionResult ....()
{
var rootobject =new Rootobject();
//set values
...
...
return Json(new
{
Rootobject= rootobject
});
}
2. in js
$.ajax({
type: 'POST',
url: //url,
dataType: 'json',
data: {
},
error: function (err) {
},
success: function (data) {
var rootobject =data.Rootobject;
...
...
},
async: true
});
Upvotes: 0