种子列表在空列表中

时间:2014-05-21 20:14:28

标签: c# asp.net-mvc list

我正在使用KnockoutMVC,它需要在VIEW中使用强类型模型。我在KnockoutMVC的网站上尝试过各种各样的示例,包括使用ENUMS,但仍无法使其工作。也许这是我的模型设置的问题。

模型

public class PhoneNumber
{
    public List<NumberTypeClass> Types { get; set; }
    //public NumberType enumType { get; set; }
    //public enum NumberType
    //{
    //    Work,
    //    Home,
    //    Mobile,
    //    Fax
    //}
    private string _number;
    [StringLength(14, MinimumLength = 10, ErrorMessage = "Please use (123) 456-7890 format"), Required]
    public string Number
    {
        get
        {
            this._number = BeautifyPhoneNumber(this._number);
            return this._number;
        }
        set
        {
            this._number = value;
        }
    }

    public string Extension { get; set; }
    public static String BeautifyPhoneNumber(string numberToBeautify)
    {
        //beautifyNumberCode
    } 
}

public class NumberTypeClass
{
    public int Id { get; set; }
    public string NumberType { get; set; }
}
public class VendorsEditorVendorModel
{
  public string FirstName {Get;set;}
  public string LastName {get;set;}

  public List<Address> Address {get;set;}
  public List<PhoneNumber> Phones {get;set;}
}

public class VendorsEditorModel
{
  public List<VendorsEditorVendorModel> Vendors {get;set;}
}

CONTROLLER

public class VendorsEditorController : BaseController
{
    public ActionResult CreateVendors()
            {// VendorsEditor/CreateVendors

                var vendor = new VendorsEditorModel();

                vendor.Vendors = new List<VendorsEditorVendorModel>();
                vendor.Vendors[0].Phones[0].Types = new List<NumberTypeClass>
        {
            new NumberTypeClass{Id = 0, TypeName = "Mobile"},
            new NumberTypeClass{Id = 0, TypeName = "Work"},
            new NumberTypeClass{Id = 0, TypeName = "Home"}
        };//this throws an error because there is no Vendors[0] ...but how would i populate this list for every Vendor?
                return View(vendor);
            }
}

1 个答案:

答案 0 :(得分:2)

您无法通过索引[x]调用空集合。您需要从数据库中填充您的集合,或者在您可以访问其中的项目之前。如果您只是尝试将项目添加到集合中,那么您就是这样做的:

    var vendor = new VendorsEditorModel
    {
        Vendors = new List<VendorsEditorVendorModel>
        {
            new VendorsEditorVendorModel
            {
                Phones = new List<PhoneNumber>
                {
                    new PhoneNumber
                    {
                        Types = new List<NumberTypeClass>
                        {
                            new NumberTypeClass {Id = 0, NumberType = "Mobile"}
                        }
                    }
                }
            }
        }
    };

如果您只想将类型添加到已填充的集合中,可以执行以下操作:

foreach (var phone in vendor.Vendors.SelectMany(item => item.Phones))
            {
                phone.Types = new List<NumberTypeClass>
                {
                    new NumberTypeClass{Id = 0, NumberType = "Mobile"},
                    new NumberTypeClass{Id = 0, NumberType = "Work"},
                    new NumberTypeClass{Id = 0, NumberType = "Home"}
                };
            }