如何将数组模型作为值传递?

时间:2018-03-29 13:52:26

标签: c# asp.net .net asp.net-mvc model

我有一个名为APIEntity的模型类名。并且有DataObject []。现在DataObject有10个成员我希望将成员作为值传递。我该怎么做?

这是我的模特:

public class APIEntity
{
    public List<int> unitCodes { get; set; }
    public DateTime start { get; set; }

    public DateTime end { get; set; }
    public DataObject[] fields { get; set; }
}

public class DataObject
{
    public string unitCode { get; set; }
    public string timedate { get; set; }
    public string latitude { get; set; }
    public string longitude { get; set; }
    public string ignition { get; set; }
    public string velocity { get; set; }
    public string positionerror { get; set; }
    public string digitherm5 { get; set; }
    public string direction { get; set; }
    public string distance { get; set; }
}

我只想传递值:

var dat = new APIEntity()
{
    unitCodes = new List<int>() { 19215 },
    start = DateTime.ParseExact("2018-03-12 10:00:00", "yyyy-MM-dd HH:mm:ss", null),
    end = DateTime.ParseExact("2018-03-12 11:00:00", "yyyy-MM-dd HH:mm:ss", null),
    fields = ?????? // I want to pass the members here...unitcode,timedate,bla bla....
};

1 个答案:

答案 0 :(得分:1)

试试这个:

var dat = new APIEntity()
{
    unitCodes = new List<int>() { 19215 },
    start = DateTime.ParseExact("2018-03-12 10:00:00", "yyyy-MM-dd HH:mm:ss", null),
    end = DateTime.ParseExact("2018-03-12 11:00:00", "yyyy-MM-dd HH:mm:ss", null),
    fields = new DataObject[] 
    {
        new DataObject { unitCode = "unitCode", timedate  = "timedate " }, 
        new DataObject { unitCode = "unitCode", timedate  = "timedate " }
    }
};