2013-03-11 87 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