I have a page with many textboxes, named from textbox1 to textbox 10 for example. Is there a way to pass them to the controller as an array. I thought about serializing the form to a json string and passing the string to the controller, but is there a better
way ?
The above approach will not work with the MVC framework - it is specific to webforms.
I'm currently using Castle.Components.Binder (http://www.castleproject.org) to do this. Hammett provides a sample project
here which shows how to add support for the castle DataBinder by overriding InvokeActionMethod on the controller.
Have a look at the
MVC Contrib project there is a patch for deserializing arrays. Here is one of the tests. assuming you have a bunch of textboxes named "id[0]" ..."id[10]" this would bring them back into a int array.
Thanks a lot, that code from mvccontrib did the trick, at least until something better comes along, will this maybe be included in the mvc.net in the future ?
arnig
Member
260 Points
60 Posts
Array to controllers
Dec 19, 2007 06:47 PM|LINK
I have a page with many textboxes, named from textbox1 to textbox 10 for example. Is there a way to pass them to the controller as an array. I thought about serializing the form to a json string and passing the string to the controller, but is there a better way ?
mcmcomasp
Contributor
6834 Points
1436 Posts
Re: Array to controllers
Dec 19, 2007 07:58 PM|LINK
string[] myArray = new string[10];
for(int i =0;i<10;++i){
string txtBoxname = "txtbox" + i;
TextBox tbTmp = (TextBox)this.FindControl(txtBoxname);
if(tbTmp != null){
myArray[i] = tbTmp.Text;
}
}
that wil loop through 1 - 10 and create a text box name, then finds the textbox, gets the text and adds it to an array w/ 10 spots in it.
hth,
mcm
arnig
Member
260 Points
60 Posts
Re: Array to controllers
Dec 19, 2007 08:25 PM|LINK
Well yes, but that is not what I was talking about.
I'm talking about passing the values of those controls to the controller as an array.
JeremyS
Member
506 Points
99 Posts
Re: Array to controllers
Dec 19, 2007 08:26 PM|LINK
The above approach will not work with the MVC framework - it is specific to webforms.
I'm currently using Castle.Components.Binder (http://www.castleproject.org) to do this. Hammett provides a sample project here which shows how to add support for the castle DataBinder by overriding InvokeActionMethod on the controller.
In my view I can then have several textboxes:
... and then in the controller I can have:
[ControllerAction] public void Update([FormBinder] string[] textboxes) { }... and the textboxes array will automatically be populated from the form data.
abombss
Member
575 Points
164 Posts
Re: Array to controllers
Dec 19, 2007 08:33 PM|LINK
Have a look at the MVC Contrib project there is a patch for deserializing arrays. Here is one of the tests. assuming you have a bunch of textboxes named "id[0]" ..."id[10]" this would bring them back into a int array.
+ [Test] + public void DeserializePrimitiveArray() + { + NameValueCollection collection = new NameValueCollection(); + collection["ids[0]"] = "10"; + collection["ids[1]"] = "20"; + + NameValueDeserializer nvd = new NameValueDeserializer(); + + int[] array = (int[])nvd.Deserialize(collection, "ids", typeof(int[])); + + Assert.IsNotNull(array); + Assert.AreEqual(2, array.Length); + Assert.AreEqual(10, array[0]); + Assert.AreEqual(20, array[1]); + }MVC binding
arnig
Member
260 Points
60 Posts
Re: Array to controllers
Dec 19, 2007 09:34 PM|LINK
Thanks a lot, that code from mvccontrib did the trick, at least until something better comes along, will this maybe be included in the mvc.net in the future ?
robconery
Participant
852 Points
195 Posts
Re: Array to controllers
Dec 19, 2007 09:43 PM|LINK
In HTML if you name controls the same, the browser will send them via POST as a string-delimited set. So
<input name ="mybox" value="Foo" /><input name="mybox" value="Bar" /> will be sent along in the header as "Foo,Bar". To use these, you can:
string myInput=Request.Form["mybox"];
string[] inputs=myInput.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
nagir
Member
162 Points
184 Posts
Re: Array to controllers
Dec 19, 2007 11:05 PM|LINK
Hi Rob,
<input name ="mybox" value="FooA,FooB" /><input name="mybox" value="Bar" />
string myInput=Request.Form["mybox"];
string[] inputs=myInput.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
What will be in inputs?
Regards,
Dmitriy.