模型绑定两个或更多个集合 [英] Model binding two or more collections

查看:98
本文介绍了模型绑定两个或更多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何运气模型使用code。通过菲尔哈克张贴在这里结合两个或两个以上的集合:的模型绑定到一个列表

Has anyone had any luck model binding two or more collections using the code posted by Phil Haack here: Model Binding To A List?

作为一个例子,我有以下code。

As an example, I have the below code.

public class Book {
  public string Name { get; set; }
}
public class Author {
  public string Name { get; set; }
}


public ActionResult Index(List<Book> books, List<Author> authors) {
  // Will never model bind two collections.
}

这是我的HTML是:

<input type="hidden" name="books.index" value="1" />
<input type="text" name="books[1].Name" />

<input type="hidden" name="books.index" value="2" />
<input type="text" name="books[2].Name" />

<input type="hidden" name="authors.index" value="1" />
<input type="text" name="authors[1].Name" />

<input type="hidden" name="authors.index" value="1" />
<input type="text" name="authors[1].Name" />

这是我得到的例外是:

参数词典共包含参数'作家'的方法'System.Web.Mvc.ActionResult指数(System.Collections.Generic.List 1 [图书],System.Collections.Generic无效的项.LIST 1 [作者])中的HomeController的。该字典包含类型的值'System.Collections.Generic.List 1 [图书]',但参数需要的类型System.Collections.Generic.List值' 1 [作者简介]。参数名:参数

The parameters dictionary contains an invalid entry for parameter 'authors' for method 'System.Web.Mvc.ActionResult Index(System.Collections.Generic.List1[Book], System.Collections.Generic.List1[Author])' in 'HomeController'. The dictionary contains a value of type 'System.Collections.Generic.List1[Book]', but the parameter requires a value of type 'System.Collections.Generic.List1[Author]'. Parameter name: parameters

我是不是做错了什么,或者这是不是ASP.NET MVC支持?

Am I doing something wrong or is this not supported by ASP.NET MVC?

推荐答案

您的问题是别的地方,我无法重现。以下正常工作对我来说:

Your problem is somewhere else, I was unable to reproduce. The following works fine for me:

型号:

public class Book
{
    public string Name { get; set; }
}
public class Author
{
    public string Name { get; set; }
}

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Index(List<Book> books, List<Author> authors)
    {
        return View();
    }
}

查看:

<% using (Html.BeginForm()) { %>
    <input type="text" name="books[0].Name" value="book 1" />
    <input type="text" name="books[1].Name" value="book 2" />

    <input type="text" name="authors[0].Name" value="author 1" />
    <input type="text" name="authors[1].Name" value="author 2" />

    <input type="submit" value="OK" />
<% } %>

它成功地结合数值回到了POST操作。

It successfully binds values back in the POST action.

更新:

我确认这是在ASP.NET MVC 3 RC2 一个错误将被固定在RTM。作为一种变通方法,你可以把在下面的的Application_Start

I confirm that this is a bug in ASP.NET MVC 3 RC2 which will be fixed in the RTM. As a workaround you could put the following in your Application_Start:

ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();

这篇关于模型绑定两个或更多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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