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)
{

}
1
  • if you are using jQuery then i can suggest you a very simple way!! Commented Aug 8, 2011 at 14:42

2 Answers 2

7

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
}
5
  • 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.. Commented Aug 8, 2011 at 11:07
  • fruits is a parameter. Do you mean you can't modify the signature of the Index action? Commented Aug 8, 2011 at 11:11
  • Yes. I dont want the signature of index. Because fruits ..etc paratmeters are persent in another business class.. Commented Aug 8, 2011 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: stackoverflow.com/questions/309115/… - That way it can be accomplished, although I don't like it. Commented Aug 8, 2011 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: <input name="fruit" value="apple" /><input name="fruit" value="orange" /> and then you will get an array in the controller called "fruit" Commented Aug 8, 2011 at 16:06
6

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.