ASP.Net MVC中for循环内的DropDownList

时间:2016-01-02 19:45:08

标签: asp.net-mvc-4 html.dropdownlistfor

我对ASP.net View中for循环中的DropDownList感到很沮丧。这是我正在尝试做的事情。我有一个名为OrganizationAddress的类,它有对象Organization,Address

  public class OrganizationAddress
{

public OrganizationAddress ()
    {
        this.address = new List<Address>();
    }

    public Organization organization { get; set; }

    public List<Address> address { get; set; }

}
public partial class Address
    {

    public int AddressID { get; set; }
    public int AddressTypeID { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string Provience { get; set; }
    public string PostalCode { get; set; }
}

控制器

public ActionResult Create()
{
OrganizationAddress organizationadd = new OrganizationAddress ();
ViewBag.AddressTypeID = new    SelectList(db.AddressTypes,"AddressTypeID","Name");
return View(organizationadd);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create( FormCollection f, List<Address> address)
{
OrganizationAddress organizationadd = new OrganizationAddress ();
ViewBag.AddressTypeID = new   SelectList(db.AddressTypes,"AddressTypeID","Name");
return View(organizationadd);
}

查看

@model panther.Models.OrganizationAddress 
for (int i = 0; i < Model.address.Count(); i++)
{
<div class="form-group">
@Html.LabelFor(model => model.address[i].AddressTypeID, "AddressTypeID",    htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("AddressTypeID",new SelectList("[" + i +  "].AddressTypeID", "AddressTypeID", "Name"))
</div>
</div>
}

我得到“DataBinding:'System.Char'不包含名为'AddressTypeID'的属性。”你能帮助我找出我在这里做的错误吗?谢谢

0 个答案:

没有答案