Asp.net 复杂类型的模型绑定器
我有以下型号:Asp.net 复杂类型的模型绑定器,asp.net,asp.net-mvc-3,Asp.net,Asp.net Mvc 3,我有以下型号: public class Customer { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public Address BillingAddress { get; set; } public Address ShippingAdd
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address BillingAddress { get; set; }
public Address ShippingAddress { get; set; }
}
地址为:
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string City { get; set; }
}
我想创建一个表单/视图,我已经创建了视图:
创造
请建议解决方案创建强类型视图。客户类型。简单解决方案,覆盖地址上的ToString()
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string City { get; set; }
public override ToString()
{
return string.Format("{0} {1}",Street,City);
}
}
在你看来:
@model Customer
<div>
<label>First Name</label>
@Html.TextBoxFor<Customer,string>(m => m.Firstname)
</div>
<div>
<label>Last Name</label>
@Html.TextBoxFor<Customer,string>(m => m.Firstname)
</div>
<div>
<label>Billing Address</label>
@Html.TextBoxFor<Customer,string>(m => m.BillingAddress)
</div>
<div>
<label>Shipping Address</label>
@Html.TextBoxFor<Customer,string>(m => m.ShippingAddress)
<input name="ShippingAddress" />
</div>
<div>
<input type="submit" value="Save" />
</div>
@model客户
名字
@Html.TextBoxFor(m=>m.Firstname)
姓
@Html.TextBoxFor(m=>m.Firstname)
帐单地址
@Html.TextBoxFor(m=>m.BillingAddress)
送货地址
@Html.TextBoxFor(m=>m.ShippingAddress)
如果不使用强类型模型,我是否可以执行此操作?我的意思是没有@Html.TextBoxFor(m=>m.ShippingAddress)。我已经为address创建了一个局部视图并使用了它,但是post-create方法没有得到address值
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string City { get; set; }
public override ToString()
{
return string.Format("{0} {1}",Street,City);
}
}
@model Customer
<div>
<label>First Name</label>
@Html.TextBoxFor<Customer,string>(m => m.Firstname)
</div>
<div>
<label>Last Name</label>
@Html.TextBoxFor<Customer,string>(m => m.Firstname)
</div>
<div>
<label>Billing Address</label>
@Html.TextBoxFor<Customer,string>(m => m.BillingAddress)
</div>
<div>
<label>Shipping Address</label>
@Html.TextBoxFor<Customer,string>(m => m.ShippingAddress)
<input name="ShippingAddress" />
</div>
<div>
<input type="submit" value="Save" />
</div>