Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I have a comma seperated string in a textbox, I want to pass this string as string array to an action method. Could any one tell me how can I acheive 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)
{

}
share|improve this question
    
if you are using jQuery then i can suggest you a very simple way!! – Praveen Prasad Aug 8 '11 at 14:42
up vote 5 down vote accepted

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.

share|improve this answer
    
Thanks, I will try this. – Lamps Aug 9 '11 at 15:22
    
Ok, please let me know whether it worked for you or not. – gusztav.varga.dr Aug 10 '11 at 9:14
    
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.ValueProviderResul‌​t>' could be found (are you missing a using directive or an assembly reference?) – Lamps Aug 10 '11 at 11:24
    
I am using MVC 1.0 – Lamps Aug 10 '11 at 11:25
    
For fixing this I modified to use like this, bindingContext.ValueProvider[bindingContext.ModelName].AttemptedValue; – Lamps Aug 10 '11 at 11:49

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.