1

我有以下代码,现有客户。我需要创建一个创建订单的表单,并将其传递给我的控制器。

我尝试将该页面上的模型类型更改为 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" />
}
4

2 回答 2

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"

然后就像我们写的一样:

stocks = new Dictionary<string, Company>() {
  { "MSFT", new Company() { CompanyName = "Microsoft Corporation", Industry = "Computer Software" } },
  { "AAPL", new Company() { CompanyName = "Apple, Inc.", Industry = "Consumer Devices" } }
};
于 2013-03-11T01:32:04.407 回答
0

这个IMO是你应该如何处理这个:

如果您真的不想制作视图模型,请在您的视图包中传递 customerID(ps 可以制作视图模型!)

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

你的发布方法:

[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" />
}
于 2013-03-11T01:40:41.193 回答