简体   繁体   English

MVC.Net绑定复杂模型?

[英]MVC.Net binding complex models?

I have the following code, with an existing customer. 我有以下代码,并且已有客户。 I need to create a form that created an Order, and pass it to my Controller. 我需要创建一个创建订单的表单,并将其传递给我的控制器。

I tried changing my model type on that page to Order instead of Customer, but then I'm gonna have to pass the Order object as well, or at least the OrderId. 我尝试将页面上的模型类型更改为Order而不是Customer,但随后我也必须传递Order对象,或者至少传递OrderId。

Models 楷模

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; }
}

Controller 控制者

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);
   }
}

View 视图

@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" />
}

I suppose you're looking for what this blog article describes: ASP.NET MVC Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries 我想您正在寻找此博客文章描述的内容: 用于模型绑定到数组,列表,集合,字典的ASP.NET MVC线格式

Quote from the blog: 引用博客:

If the signature looks like this: 如果签名看起来像这样:

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

And we are given this in HTML: 我们用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" /> 

Which like this: 像这样:

 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" 

Then it will be just as if we had written: 然后就好像我们已经写过:

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

This IMO is how you should be handling this: 这是您应该如何处理的IMO:

Pass customerID in your viewbag if you're really not wanting to make a viewmodel (ps It is ok to make a viewmodel!) 如果您确实不想创建视图模型,请在您的视图包中传递customerID(ps可以创建视图模型!)

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

your post method: 您的发布方法:

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

your cshtml 您的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" />
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM