Puis-je utiliser la liste ou quelque chose? P>
7 Réponses :
Scott Hanselman a un excellent tutoriel pour ce faire ici. Strong> a> p>
Phil Haack dispose d'une version mise à jour avec un projet de téléchargement. HAACKED.COM / ARARCHIVE/2008/10/ 23 / Modèle-Binding-to-A-List.aspx
Prenez simplement la bonne sorte de collection. Exactement quel type dépend de quelle version: p>
MVC1: Action du public Dosomething (int [] entrée) code>
MVC2: Action du public Dosomething Dosomething (IList
Vous pouvez passer une liste
[ArrayOrListParameterAttribute("ids", ",")]
public ActionResult Index(List<string> ids)
{
}
Aucun attribut de ce type. Pas dans .NET, et googlé et n'a trouvé aucun code source nulle part.
Vous devez les transmettre à votre action via l'ajout de chaque entiers à votre message ou obtenez-vous de la querystring comme: puis dans votre action, vous aurez l'action suivante p> XXX PRE> MVC remplacera ensuite la liste avec 1,4, et 6 P> une chose à conscience. Votre chaîne de requête ne peut pas avoir de crochets en eux. Parfois, lorsque les listes JavaScript sont formées, votre chaîne de requête ressemblera à ceci: P> myints[]=1&myints[]=4&myints[]=6
Certainement ne fonctionne pas pour moi. Il suffit d'obtenir un gros gras null code> pour la liste list
Si vous essayez d'envoyer la liste à partir d'un élément d'interface (comme, une table), vous pouvez simplement définir leur attribut de nom dans HTML sur: CollectionName [Index]
Par exemple: et p> Le paramètre Intlist Wil reçoit une liste contenant 1 et 2 dans cet ordre P> p>
Utilisation:
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
}
Si aucun des autres postes de ce fil ne fonctionne et que vous ne voulez pas passer beaucoup de temps à apprendre les idiosyncrasies de paramètres MVC en passant, voici une solution parfaitement bonne. Dans JavaScript, stringifiez la liste: puis désériorialisez-les dans le contrôleur: p>
Comment avez-vous l'intention de passer la liste? à partir d'un appel client (JavaScript) ou d'une page de vue?
+1 pour la question la plus courte que j'ai vue sur ce site. :)