2010-08-10 50 views

回答

7

Scott Hanselman在这样做here.

+2

Phil Haack有一个更新的版本,下载项目。 http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx – 2010-08-11 12:26:27

1

只要采取正确的类集合的优秀教程。究竟哪种类型取决于哪一个版本:

MVC1:public ActionResult DoSomething(int[] input)
MVC2:public ActionResult DoSomething(IList<int> input)

+0

您可以将一个'List '传递给MVC 1中的一个操作方法。 – DaveDev 2010-08-10 21:54:31

-1
[ArrayOrListParameterAttribute("ids", ",")] 
public ActionResult Index(List<string> ids) 
{ 

} 
+0

没有这样的属性。不在.NET中,并且使用Google搜索,没有发现任何源代码。 – AaronLS 2013-05-29 19:41:52

8

您需要通过添加到您的POST每个整数将它们传递给你的行动或GET查询字符串,像这样:

myints=1&myints=4&myints=6 

然后在你的行动,你将有以下作用

public ActionResult Blah(List<int> myints) 

MVC然后将填充列表1,4和6

有一点要注意。你的查询字符串不能有括号。有时,当形成的JavaScript列出您的查询字符串会是这样的:

myints[]=1&myints[]=4&myints[]=6 

这将导致您的列表为空(或具有计数为零)。括号不能在MVC中正确绑定模型。

1

如果你正试图从一些界面项(如,表)发送列表,你可以设置在HTML的name属性:集合名[索引] 例如:

<input id="IntList_0_" name="IntList[0]" type="text" value="1" /> 
<input id="IntList_1_" name="IntList[1]" type="text" value="2" /> 

public ActionResult DoSomething(List<int> IntList) { 
} 

的intList中参数港岛线接收的顺序

0

使用含有1和2的列表:

[ArrayOrListParameterAttribute("ids", ",")] 
public ActionResult Index(List<string> ids) 
{ 

} 

这里的代码ArrayOrListParameterAttribute

using System; 
using System.Web.Mvc; 
using System.Reflection; 
using System.Collections; 
using System.Collections.Generic; 

namespace MvcApplication1 
{ 
public class ArrayOrListParameterAttribute : ActionFilterAttribute 
{ 
    #region Properties 

    /// <summary> 
    /// Gets or sets the name of the list or array parameter. 
    /// </summary> 
    /// <value>The name of the list or array parameter.</value> 
    private string ListOrArrayParameterName { get; set; } 

    /// <summary> 
    /// Gets or sets the separator. 
    /// </summary> 
    /// <value>The separator.</value> 
    private string Separator { get; set; } 

    #endregion 

    #region Constructors 

    /// <summary> 
    /// Initializes a new instance of the <see cref="ArrayOrListParameterAttribute"/> class. 
    /// </summary> 
    /// <param name="listOrArrayParameterName">Name of the list or array parameter.</param> 
    public ArrayOrListParameterAttribute(string listOrArrayParameterName) : this(listOrArrayParameterName, ",") 
    { 

    } 

    /// <summary> 
    /// Initializes a new instance of the <see cref="ArrayOrListParameterAttribute"/> class. 
    /// </summary> 
    /// <param name="listOrArrayParameterName">Name of the list or array parameter.</param> 
    /// <param name="separator">The separator.</param> 
    public ArrayOrListParameterAttribute(string listOrArrayParameterName, string separator) 
    { 
     ListOrArrayParameterName = listOrArrayParameterName; 
     Separator = separator; 
    } 

    #endregion 

    #region Public Methods 

    /// <summary> 
    /// Called when [action executing]. 
    /// </summary> 
    /// <param name="filterContext">The filter context.</param> 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     string separatedValues = filterContext.RouteData.GetRequiredString(ListOrArrayParameterName); 
     ParameterInfo[] parameters = filterContext.ActionMethod.GetParameters(); 

     ParameterInfo searchedParameter = Array.Find(parameters, parameter => parameter.Name == ListOrArrayParameterName); 

     if (searchedParameter == null) 
      throw new InvalidOperationException(string.Format("Could not find Parameter '{0}' in action method '{1}'", ListOrArrayParameterName, filterContext.ActionMethod.Name)); 

     Type arrayOrGenericListType = searchedParameter.ParameterType; 

     if (!IsTypeArrayOrIList(arrayOrGenericListType)) 
      throw new ArgumentException("arrayOrIListType is not an array or a type implementing Ilist or IList<>: " + arrayOrGenericListType); 

     filterContext.ActionParameters[ListOrArrayParameterName] = GetArrayOrGenericListInstance(arrayOrGenericListType, separatedValues, Separator); 

     base.OnActionExecuting(filterContext); 
    } 

    #endregion 

    #region Non Public Methods 

    private static bool IsTypeArrayOrIList(Type type) 
    { 
     if (type.IsArray) 
      return true; 

     return (Array.Exists(type.GetInterfaces(), x => x == typeof(IList) || x == typeof(IList<>))); 
    } 

    private static object GetArrayOrGenericListInstance(Type arrayOrIListType, string separatedValues, string separator) 
    { 
     if (separatedValues == null) 
      return null; 

     if (separator == null) 
      throw new ArgumentNullException("separator"); 

     if (arrayOrIListType.IsArray) 
     { 
      Type arrayElementType = arrayOrIListType.GetElementType(); 
      ArrayList valueList = GetValueList(separatedValues, separator, arrayElementType); 

      return valueList.ToArray(arrayElementType); 
     } 

     Type listElementType = GetListElementType(arrayOrIListType); 

     if (listElementType != null) 
      return GetGenericIListInstance(arrayOrIListType, GetValueList(separatedValues, separator, listElementType)); 

     throw new InvalidOperationException("The type could not be handled, this should never happen: " + arrayOrIListType); 
    } 

    private static Type GetListElementType(Type genericListType) 
    { 
     Type listElementType = null; 

     foreach (Type type in genericListType.GetInterfaces()) 
     { 
      if (type.IsGenericType && type == typeof(IList<>).MakeGenericType(type.GetGenericArguments()[0])) 
      { 
       listElementType = type.GetGenericArguments()[0]; 
       break; 
      } 
     } 

     return listElementType; 
    } 

    private static object GetGenericIListInstance(Type arrayOrIListType, ArrayList valueList) 
    { 
     object result = Activator.CreateInstance(arrayOrIListType); 
     const BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public; 

     foreach (object value in valueList) 
     { 
      arrayOrIListType.InvokeMember("Add", flags, null, result, new[] { value }); 
     } 

     return result; 
    } 

    private static ArrayList GetValueList(string separatedValues, string separator, Type memberType) 
    { 
     string[] values = separatedValues.Split(new[] { separator }, StringSplitOptions.RemoveEmptyEntries); 

     ArrayList valueList = new ArrayList(); 

     foreach (string value in values) 
     { 
      valueList.Add(Convert.ChangeType(value, memberType)); 
     } 

     return valueList; 
    } 

    #endregion 
} 

}

相关问题