模型粘合剂
问题描述:
我有以下型号:模型粘合剂
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; }
}
其中Address是:
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();
}
请建议的解决方案
答
简单的解决方案,在地址重写的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>
答
创建强类型视图。客户类型。
着我去做,而无需使用强类型的模型?我的意思是没有@ Html.TextBoxFor(m => m.ShippingAddress)。我创建了地址的部分视图并使用它,但后创建方法不获取地址值 –
DotnetSparrow
2011-04-15 20:21:39