9
votes

Comment adopter une liste d'entiers à une action MVC?

Puis-je utiliser la liste ou quelque chose?


2 commentaires

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. :)


7 Réponses :



1
votes

Prenez simplement la bonne sorte de collection. Exactement quel type dépend de quelle version:

MVC1: Action du public Dosomething (int [] entrée)
MVC2: Action du public Dosomething Dosomething (IList Entrée)


1 commentaires

Vous pouvez passer une liste dans une méthode d'action dans MVC 1.



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

 }

1 commentaires

Aucun attribut de ce type. Pas dans .NET, et googlé et n'a trouvé aucun code source nulle part.



11
votes

Vous devez les transmettre à votre action via l'ajout de chaque entiers à votre message ou obtenez-vous de la querystring comme: xxx pré>

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


1 commentaires

Certainement ne fonctionne pas pour moi. Il suffit d'obtenir un gros gras null pour la liste list objet.



3
votes

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: xxx

et xxx

Le paramètre Intlist Wil reçoit une liste contenant 1 et 2 dans cet ordre


0 commentaires

0
votes

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
}


0 commentaires

1
votes

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: xxx

puis désériorialisez-les dans le contrôleur: xxx


0 commentaires