我有一个DTO(或POCO类的,不确定的,正确的说法是什么)类(内型)具有以下值:硬编码值
public class MainDTO
{
public CustomerDTO[] customer { get; set; }
public string itemPurchased { get; set; }
public int staffId { get; set; }
}
public class CustomerDTO
{
public string name { get; set; }
public string[] address { get; set; }
}
我在我的MVC调用此应用程序在REST web服务调用后返回一个带有上述类的值的Json。
我想在控制器中对这些值进行硬编码,以便我可以测试视图。但是,我不知道如何硬编码调用另一个类的值。
//Response variable below contains Json result of the web service call
var root = JsonConvert.DeserializeObject<GETModel.MainDTO>(response);
//Hard-coding values to test view
root.customer = ? //How can I hard-code this value?
root.itemPurchased = “Random Item”;
root.staffId = 2299;
return View(root);
如何硬编码“root.customer”变量?它包含一个数组和字符串变量。
创建一个类的实例,设置它的变量,你要硬编码值,并对其进行序列化 - 你在做反序列化的正好相反.. – Aaron
'root.customer'是一个数组。你想要设置什么?只是一个空阵列? '新的CustomerDTO [0]'? – David
您可以将它设置为空数组,硬编码值的数组等。您的选择是您的。 'root.customer = new CustomerDTO [0];''或'root.customer = new [] {new CustomerDTO(){name =“John Smith”,address = new [] {“123 Main St”,“Washington DC” }};' – maccettura