我有以下代码,现有客户。我需要创建一个创建订单的表单,并将其传递给我的控制器。
我尝试将该页面上的模型类型更改为 Order 而不是 Customer,但是我还必须传递 Order 对象,或者至少是 OrderId。
楷模
public class Customer
{
public int Id { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
public List<Order> Orders { get; set;}
}
public class Order
{
public int Id { set; get; }
public string ItemName { set; get; }
public DateTime SomeDate { set; get; }
}
控制器
public class OrderController : Controller
{
[HttpPost]
public Create ActionResult(Customer customer)
{
// Customer doesn't have the values that it suppose to have
// All fields including the array of orders are null
customerStorage.UpdateOrders(customer);
return ("ItemListPartial", customer.orders);
}
}
看法
@model Company.Application.Model.Customer;
@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" }))
{
// How can I bind this to Order object ?
// I can't define model as Order, because then I need to pass Customer Separately
@Html.TextBoxFor(o => o.Orders[0].ItemName)
@Html.TextBoxFor(o => o.Orders[0].SomeDate)
<input type="submit" value="Add" />
}