2013-03-11 84 views
1

我有以下代碼和現有客戶。 我需要創建一個創建訂單的表單,並將其傳遞給我的控制器。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" /> 
} 
+0

如果要創建一個訂單,你的對象應該是一個訂單。您是否想要將其與客戶相關聯?不知道爲什麼你不使用Order對象進行綁定。 – 2013-03-11 01:17:22

+0

'公共ActionResult(客戶客戶)'這是一個錯字?缺少函數名稱。 – 2013-03-11 01:18:41

+0

@Ravi謝謝,這是一個固定它的類型。 – aryaxt 2013-03-11 01:30:04

回答

1

我想你在尋找什麼這個博客文章介紹: ASP.NET MVC Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries

從博客

報價:

如果簽名是這樣的:

public ActionResult Blah(IDictionary<string, Company> stocks) { 
    // ... 
} 

,我們在HTML給出這樣的:

<input type="text" name="stocks[0].Key" value="MSFT" /> 
<input type="text" name="stocks[0].Value.CompanyName" value="Microsoft Corporation" /> 
<input type="text" name="stocks[0].Value.Industry" value="Computer Software" /> 
<input type="text" name="stocks[1].Key" value="AAPL" /> 
<input type="text" name="stocks[1].Value.CompanyName" value="Apple, Inc." /> 
<input type="text" name="stocks[1].Value.Industry" value="Consumer Devices" /> 

這是這樣的:

stocks[0].Key = "MSFT" 
stocks[0].Value.CompanyName = "Microsoft Corporation" 
stocks[0].Value.Industry = "Computer Software" 
stocks[1].Key = "AAPL" 
stocks[1].Value.CompanyName = "Apple, Inc." 
stocks[1].Value.Industry = "Consumer Devices" 

然後就好像我們有wr伊頓:

stocks = new Dictionary<string, Company>() { 
    { "MSFT", new Company() { CompanyName = "Microsoft Corporation", Industry = "Computer Software" } }, 
    { "AAPL", new Company() { CompanyName = "Apple, Inc.", Industry = "Consumer Devices" } } 
}; 
0

這IMO是如何你應該處理這樣的:在您的viewbag

通行證的customerID如果你真的不想做一個視圖模型(附:它是確定做一個視圖模型)

public class OrderController : Controller 
{ 
    public ActionResult Create(int customerID) 
    { 
     ViewBag.CustomerID = customerID 
     Order ord = new Order(o); 
     return View(ord); 
    } 
} 

您的POST方法:

[HttpPost] 
public ActionResult Create(int CustomerID, Order order) 
{ 
    //create your order 
} 

您CSHTML

@model Company.Application.Model.Order; 

@using (Ajax.BeginForm(new AjaxOptions { UpdateTargetId = "result" })) 
{ 
    @Html.TextBoxFor(o => o.ItemName) 
    @Html.TextBoxFor(o => o.SomeDate) 
    @Html.Hidden("CustomerID",ViewBag.CustomerID) 
    <input type="submit" value="Add" /> 
} 
+0

爲什麼不能使用只有一個具有Customer屬性和Order屬性(以及任何其他所需)的新模型類。這樣你就可以將兩者結合起來並更容易地將它們聯繫起來。您不僅僅限於與您的數據庫匹配的模型。我一直這樣做。 – 2013-03-11 04:26:07

+0

我通常會爲我的所有模型製作一個視圖模型,但我確實提到了它,但OP似乎並不想這麼做,所以我只是對它進行了評論。另外,我認爲通過ViewBag傳遞兩個項目是可以接受的。如果它是3或更多,我可能會更強烈地推薦一個比我在這裏更明確的視圖模型。 – 2013-03-11 12:42:06

+0

事實上,儘管ViewModel爲我的經驗增加了一個額外的層,但對於複雜的應用程序來說,這是值得的。我一直在使用ViewModel模式和AutoMapper,到目前爲止它確實非常棒。 – harsimranb 2014-03-03 20:24:28