我有以下型号:
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; }
}
我想创建一个表单/视图,我创建了视图:
创建
<div>
<label>First Name</label>
<input name="FirstName" />
</div>
<div>
<label>Last Name</label>
<input name="LastName" />
</div>
<div>
<label>Billing Address</label>
<input name="BillingAddress" />
</div>
<div>
<label>Shipping Address</label>
<input name="ShippingAddress" />
</div>
<div>
<input type="submit" value="Save" />
</div>
</form>
我想要显示完整的地址,我的意思是城市和街道。我需要创建局部视图还是什么?我需要将此视图发布到以下actionResult:
[HttpPost]
public ActionResult Create(Customer customerToCreate)
{
return View();
}
请建议解决方案
答案 0 :(得分:0)
创建强类型视图。客户类型。
答案 1 :(得分:0)
简单解决方案,覆盖地址上的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>