2

I have a comma separated string in a textbox, I want to pass this string as string array to an action method. Could anyone tell me how can I achieve this. thanks.

I am using MVC 1.0.

Views:

<input type="text" name="fruits" /> -- Contains the comma seperated values

Action method

public ActionResult Index(string[] fruits)
{

}
Rodia
  • 1,407
  • 8
  • 22
  • 29
Bhaskar
  • 1,680
  • 7
  • 26
  • 40

2 Answers2

7

You can create a custom model binder to achieve this. Create a class like this to do the split.

public class StringSplitModelBinder : IModelBinder
{
    #region Implementation of IModelBinder

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (!bindingContext.ValueProvider.ContainsKey(bindingContext.ModelName))
        {
            return new string[] { };
        }

        string attemptedValue = bindingContext.ValueProvider[bindingContext.ModelName].AttemptedValue;
        return !String.IsNullOrEmpty(attemptedValue) ? attemptedValue.Split(',') : new string[] { };
    }

    #endregion
}

Then you can instruct the framework to use this model binder with your action like this.

public ActionResult Index([ModelBinder(typeof(StringSplitModelBinder))] string[] fruits)
{
}

Instead of applying the ModelBinder attribute to action method parameters you can also register the custom model binder globally in the application start method of your Global.asax.

ModelBinders.Binders.Add(typeof(string[]), new StringSplitModelBinder());

This will split the single string value posted to the array you require.

Please note that the above was created and tested using MVC 3 but should also work on MVC 1.0.

  • I am getting this error.. System.Collections.Generic.IDictionary' does not contain a definition for 'GetValue' and no extension method 'GetValue' accepting a first argument of type 'System.Collections.Generic.IDictionary' could be found (are you missing a using directive or an assembly reference?) – Bhaskar Aug 10 '11 at 11:24
  • For fixing this I modified to use like this, bindingContext.ValueProvider[bindingContext.ModelName].AttemptedValue; – Bhaskar Aug 10 '11 at 11:49
  • Your code doesn't handles the null case scenario? So its throwing object reference error when null value is passed. – Bhaskar Aug 10 '11 at 11:50
  • I've updated the code of the model binder, please check if it works now. – gusztav.varga.dr Aug 10 '11 at 13:42
  • Thanks a Lot for taking time to explain these stuffs... +1 and I will mark it as answer.. – Bhaskar Aug 10 '11 at 15:41
6

Pass the string (with the commas) of your textbox directly to the controller action and create the array inside of the action.

public ActionResult Index(string fruits)
{
    var fruitsArray = fruits.Split(',');
    // do something with fruitArray
}
Sandro
  • 2,998
  • 2
  • 25
  • 51
  • thanks... But I dont want this solution. Is there any other way? I know this solution. MY requirement is like that. I just want to know other ways of achieving this.. – Bhaskar Aug 08 '11 at 11:07
  • fruits is a parameter. Do you mean you can't modify the signature of the Index action? – Sandro Aug 08 '11 at 11:11
  • Yes. I dont want the signature of index. Because fruits ..etc paratmeters are persent in another business class.. – Bhaskar Aug 08 '11 at 12:35
  • When you can't modify the controller to change or overload the Index action, I only see a solution with JavaScript. Like here: http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form - That way it can be accomplished, although I don't like it. – Sandro Aug 08 '11 at 13:01
  • Depending on the reason that you have a comma separated list, you could also repeat the input tag with one value per tag: and then you will get an array in the controller called "fruit" – Jeremy Bell Aug 08 '11 at 16:06