我在将模型从视图上的表单发送到控制器时遇到问题。主类 I 中有数据,但子类丢失了它们的值。
我有一个像这样的主要报价类:
public class Quotation
{
public string QuotationID { get; set; }
public TaxiPartner taxiPartner { get; set; }
public TaxiCompany taxiCompany { get; set; }
public int maxNumberOfSeats { get; set; }
public int maxNumberOfBags { get; set; }
public double price { get; set; }
}
然后例如我有 TaxiPartner 类:
public class TaxiPartner
{
public string Id { get; set; }
public string Name { get; set; }
public List<Address> Addresses { get; set; }
public bool supportsPasswordReset { get; set; }
public bool supportsValidatebysms { get; set; }
}
这是我认为的表格
@if (Model.listOfQuotations.Count > 0)
{
<h3>You can book online with:</h3>
foreach (var item in Model.listOfQuotations)
{
using (Html.BeginForm("BookingPage1", "SearchResults", FormMethod.Post))
{
<div class="well">
<div>
<h4>@item.taxiCompany.Name</h4>
</div>
<div style="float: right;">
<h2>@string.Format("{0:C}", item.price)</h2>
</div>
<div>
<b>Product Type:</b>
<img style="width: 15px;" src="~/Content/images/icon-@item.taxiPartner.cabforceProductType-orange-40px.png" />
(1 = Best Value, 2 = Executive, 3 = Minibus)
</div>
<div><b>Services:</b> @item.taxiCompany.services (0 = Outdoor pickup, 1 = Meet & greet pickup)</div>
<div><b>Maximum Number of Seats:</b> @item.maxNumberOfSeats <b>Maximum Number of Bags:</b> @item.maxNumberOfBags</div>
<div>@Html.ActionLink("Book Now", "BookingPage1", item, new { @class = "btn btn-success" })</div>
</div>
}
}
}
然后我在我的控制器中有以下操作:
[HttpPost]
public ActionResult BookingPage1(Quotation chosenQuote)
{
return View();
}
当我的控制器动作被调用时,我可以看到 QuotationID、maxNumberOfSeats、maxNumberOfBags 和 price 的值,但taxPartner 和taxCompany 中包含的值都是NULL?
我已经调试过,视图中“item”的值确实包含我需要的值,但是一旦到达控制器,它们就消失了。
谁能看到我做错了什么?