【问题标题】:ASP.Net MVC: Submit array/collection in a single parameterASP.Net MVC:在单个参数中提交数组/集合
【发布时间】:2011-02-10 17:19:22
【问题描述】:

是否可以(以及如何)使用存储在单个参数中的数组发送发布请求?喜欢

myStringArray=你好,世界

以及接受此参数作为以, 作为分隔符的数组的操作

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

参数 myStringArray 的格式无关紧要。但它必须是单个参数。

谢谢

【问题讨论】:

  • 不会是 myStringArray=hello&myStringArray=world 吗?
  • 如果它不是实际表单的一部分,我会使用 ViewModel

标签: c# .net asp.net-mvc asp.net-mvc-2 model-binding


【解决方案1】:

取决于您如何将数据发送到服务器。如果您从 url 参数或普通文本框执行此操作,其中包含如下数据:

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

那么就不需要数组参数了,把字符串用逗号分割成数组即可:

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

但是如果你是通过 AJAX 发送的,你也可以直接发送。如果你想发送一个真正的数组,那么你的 javascript 应该看起来像这样answer

【讨论】:

    【解决方案2】:

    这是我一直用于此场景的 IModelBinder。

        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;
        }
    

    【讨论】:

      【解决方案3】:

      如果你创建一个包含数组的模型,你应该没有问题。那么当然你需要一个使用强类型的视图(点击“创建强类型视图”并在列表中找到你刚刚创建的视图模型)你应该没有问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-06
        相关资源
        最近更新 更多