我有以下代碼和現有客戶。 我需要創建一個創建訂單的表單,並將其傳遞給我的控制器。MVC.Net綁定複雜模型?
我試着將該頁面上的模型類型更改爲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" />
}
如果要創建一個訂單,你的對象應該是一個訂單。您是否想要將其與客戶相關聯?不知道爲什麼你不使用Order對象進行綁定。 – 2013-03-11 01:17:22
'公共ActionResult(客戶客戶)'這是一個錯字?缺少函數名稱。 – 2013-03-11 01:18:41
@Ravi謝謝,這是一個固定它的類型。 – aryaxt 2013-03-11 01:30:04