2010-08-10 7 views

Odpowiedz

7

Scott Hanselman ma doskonały poradnik dla wykonywania tego here.

+2

Phil Haack ma zaktualizowaną wersję, z projektem pobierania. http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx –

1

Wystarczy wziąć odpowiedni rodzaj kolekcji. Dokładnie jakiego rodzaju zależy od wersji:

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

+0

Możesz przekazać 'List ' do metody akcji w MVC 1. – DaveDev

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

} 
+0

Brak takiego atrybutu. Nie w .NET, a także w Google i nie znalazł żadnego kodu źródłowego. – AaronLS

8

Trzeba przekazać je do działania poprzez dodanie każdej liczby całkowitej do POST lub GET kwerendy tak:

myints=1&myints=4&myints=6 

Następnie w swoim działaniu będzie miał następujące działania

public ActionResult Blah(List<int> myints) 

Następnie MVC zapełni listę wartością 1,4, a 6

Jedną z rzeczy, o których należy pamiętać. Twój ciąg zapytania NIE może zawierać nawiasów. Czasami, gdy tworzone są wykazy javascript ciąg kwerendy będzie wyglądać następująco:

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

To spowoduje, Lista, aby być null (lub mają liczbę zero). W nawiasie nie może być miejsca, aby MVC poprawnie wiązało model.

1

Jeśli próbujesz wysłać listy z jakiegoś elementu interfejsu (jak, stolik), można po prostu ustawić swój atrybut name w HTML do: CollectionName [Index] na przykład:

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

i

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

parametr IntList wil otrzymać listę zawierającą 1 i 2 tej kolejności

0

Zastosowanie:

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

} 

Oto kod dla 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 
} 

}

Powiązane problemy