开发者

ASP.Net MVC: Submit array/collection in a single parameter

开发者_如何学JAVAIs it possible (and how) to send a post request with an array stored in a single parameter? like

myStringArray=hello,world

and an action that accepts this parameter as an array with , as separator

public ActionResult MyAction(string[] myStringArray)
{
  //myStringArray[0] == "hello" and myStringArray[1] == "world"
}

the format of the parameter myStringArray doesn't matter. But it has to be a single parameter.

Thank you


Depends on how you are sending the data to the server. If you are doing this from a url param or normal textbox which has data like:

<input id="myString" name="myString" type="text" value="hello,world" />

Then you dont need an array parameter, just split the string by commas into an array:

public ActionResult MyAction(string myString)
{
    string[] myStringArray = myString.Split(',');
}

But if you are sending this by AJAX, you also send it directly. If you want to send a real array, then your javascript should look like this answer.


Here is an IModelBinder that I've been using for this scenario.

    public class DelimitedArrayModelBinder : IModelBinder
    {
        public DelimitedArrayModelBinder()
            : this(null)
        {
        }

        public DelimitedArrayModelBinder(params string[] delimiters)
        {
            m_delimiters = delimiters != null && delimiters.Any()
                ? delimiters
                : new[] { "," };
        }

        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            // type must be an array
            if (!bindingContext.ModelType.IsArray)
                return null;

            // array must have a type
            Type elementType = bindingContext.ModelType.GetElementType();
            if (elementType == null)
                return null;

            // value must exist
            ValueProviderResult valueProviderResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
            if (valueProviderResult == null)
                return null;

            string strValue = valueProviderResult.AttemptedValue;
            if (string.IsNullOrEmpty(strValue))
                return null;

            List<object> items = new List<object>();
            foreach (string strItem in strValue.Split(m_delimiters, StringSplitOptions.RemoveEmptyEntries))
            {
                try
                {
                    object item = Convert.ChangeType(strItem, elementType);
                    items.Add(item);
                }
                catch (Exception)
                {
                  // if we can't convert then ignore or log
                }
            }

            // convert the list of items to the proper array type.
            Array result = Array.CreateInstance(elementType, items.Count);
            for (int i = 0; i < items.Count; i++)
                result.SetValue(items[i], i);

            return result;
        }

        private readonly string[] m_delimiters;
    }


If you create a model with an array in it you should have no trouble. Then of course you nee a view that will utilize that strong type (click "Create a strongly-typed view" and find your view-model you just created in the list) you should have no problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜