MVC.Net结合复杂的模型? [英] MVC.Net binding complex models?

查看:107
本文介绍了MVC.Net结合复杂的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我试图改变我的模型类型的网页上订购,而不是客户,但我会必须通过Order对象为好,或者至少ORDERID。

模式

 公共类客户
{
   公众诠释编号{设置;得到; }
   公共字符串名字{设置;得到; }
   公共字符串名字{设置;得到; }
   公开名单<排序>订单{获得;组;}
}

公共类订单
{
   公众诠释编号{设置;得到; }
   公共ItemName字符串{设置;得到; }
   公开日期时间SomeDate {集;得到; }
}
 

控制器

 公共类OrderController:控制器
{
   [HttpPost]
   公共创建的ActionResult(客户的客户)
   {
      //客户没有,它假定有值
      //所有领域,包括订单的数组为空
      customerStorage.UpdateOrders(客户);

      返回(ItemListPartial,customer.orders);
   }
}
 

查看

  @model Company.Application.Model.Customer;

@using(Ajax.BeginForm(新AjaxOptions {UpdateTargetId =结果}))
{
    //我怎么可以绑定此订购的对象?
    //我不能定义模型订单,因为那时我需要通过客户分别
    @ Html.TextBoxFor(O => o.Orders [0] .ItemName)
    @ Html.TextBoxFor(O => o.Orders [0] .SomeDate)
    <输入类型=提交值=添加/>
}
 

解决方案

我想你要找的是什么这个博客文章介绍: <一href="http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx"相对=nofollow> ASP.NET MVC线格式模型绑定到数组,列表,集合,字典

从博客引用:

  

如果签名是这样的:

 公众的ActionResult布拉赫(IDictionary的&LT;字符串,公司&GT;股票){
  // ...
}
 

和我们在HTML中给出这样的:

 &LT;输入类型=文本名称=股[0]。重点值=MSFT/&GT;
&LT;输入类型=文本名称=股[0] .Value.CompanyName值=微软公司/&GT;
&LT;输入类型=文本名称=股[0] .Value.Industry值=计算机软件/&GT;
&LT;输入类型=文本名称=股[1]。重点值=AAPL/&GT;
&LT;输入类型=文本名称=股[1] .Value.CompanyName值=苹果公司 /&GT;
&LT;输入类型=文本名称=股[1] .Value.Industry值=消费设备/&GT;
 

     

这是这样的:

 股票[0]。重点=MSFT
个股[0] .Value.CompanyName =微软公司
个股[0] .Value.Industry =计算机软件
个股[1]。重点=AAPL
个股[1] .Value.CompanyName =苹果公司
个股[1] .Value.Industry =消费设备
 

     

然后,它会是一样,如果我们写:

 股票=新字典&LT;字符串,公司&GT;(){
  {MSFT,新公司(){公司名称=Microsoft公司,业内=计算机软件}},
  {AAPL,新公司(){公司名称=苹果公司,业内=消费设备}}
};
 

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.

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

Quote from the blog:

If the signature looks like this:

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

And we are given this in 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" } }
};

这篇关于MVC.Net结合复杂的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆