pass a collection of values to action, dynamically created textboxes - asp.net-mvc

I create textboxes dynamically with jquery. What i wonder how can i send those values into my action now with my viewmodel, kinda like this but instead of the httpPostedFileBase i want pass the values of the textboxes, I did name them like name="dTextboxes". Thanks for help im kinda stuck on this
public ActionResult Index(NewsViewModel viewModel, IEnumerable<HttpPostedFileBase> files)

You could use IEnumerable<bool> dTextboxes as action argument.
public ActionResult Index(IEnumerable<bool> dTextboxes)
assuming that in your form you have:
<input name="dTextboxes[0]" type="checkbox" value="true" />
<input name="dTextboxes[0]" type="hidden" value="false" />
<input name="dTextboxes[1]" type="checkbox" value="true" />
<input name="dTextboxes[1]" type="hidden" value="false" />
<input name="dTextboxes[2]" type="checkbox" value="true" />
<input name="dTextboxes[2]" type="hidden" value="false" />
...

I ended up doing this
public ActionResult Create(WorkViewModel viewModel, IEnumerable<string> dTextboxes)
and my dynamically textboxes looks like this
<input type="text" name="dTextboxes" />
<input type="text" name="dTextboxes" />
<input type="text" name="dTextboxes" />
and of cause in the action i do a foreach and check if string is not null after that do what i want with the string value of the textbox.

Related

ASP.NET MVC complex model retrieving on the server [duplicate]

This question already has answers here:
Collection of complex child objects in Asp.Net MVC 3 application?
(2 answers)
Closed 8 years ago.
Let's say I have model with property:
public List<string> Subscribers { get; set; }
I am rendering markup like that to receive such list on the server on form submit:
<input type="hidden" name="Subscribers[0]" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[1]" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[2]" value="SomeSubscriber" />
What if subscriber is a type with Name and ID properties. How should markup look like to get the whole list of subscribers back on form submit?
The challenge is to render markup aproppriately and without use of any editorFor templates
That would be :
<input type="hidden" name="Subscribers[0].Name" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[0].ID" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[1].Name" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[1].ID" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[2].Name" value="SomeSubscriber" />
<input type="hidden" name="Subscribers[2].ID" value="SomeSubscriber" />

MVC Razor String Concat

I am using Razor to generate a form. I want to create HTML elements based on some value from it's model property.
for example, if a model contains Id property, and I want to generate html tags as follows
<input type="hidden" name="1_chk" />
<input type="hidden" name="2_chk" />
<input type="hidden" name="3_chk" />
So I used the following syntax, and it failed. Can anyone help me out with this?
<input type="checkbox" name="#Id_chk" />
Thanks
I think this should work for you:
<input type="checkbox" name="#(Id)_chk" />
another option:
<input type="checkbox" name="#(Id + "_chk")" />

Get posted values from dynamically added controls in ASP.NET MVC

I have a form in which i'm dynamically addding controls through jQuery. I need to access the values in those controls (textboxes) when posting back the form to the server. I'm sure this is a trivial problem but i just can't get my head around it.
Any help would be greatly apreciated.
When adding a multiple controls to the page, give them all the same name attribute so you can do the following in your action:
public ActionResult MyAction(string[] items)
{
// items will contain all the values in the text boxes
return View();
}
So your HTML would like like this
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />
<input type="text" name="items" />

How to gather arbitrary length list data in ASP.NET MVC

I need to gather a list of items associated with another item from my user in a ASP.NET MVC project. I would like to have a controller action like bellow.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int x, int y, IEnumerable<int> zKeys)
{
//Do stuff here
}
How can I setup my form to pass data in this way? If data of this particular form can't be provided, what's the next best way to pass this type of information in ASP.NET MVC?
Scott Hanselman has an excellent article on how to do this here:
ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries
http://www.hanselman.com/blog/...BindingToArraysListsCollectionsDictionaries.aspx
<form action="/url" method="post">
<input name="x" type="text" value="1" />
<input name="y" type="text" value="1" />
<div>
<input name="zKeys" value="1" />
<input name="zKeys" value="2" />
<input name="zKeys" value="3" />
<input name="zKeys" value="4" />
<input name="zKeys" value="5" />
<input name="zKeys" value="6" />
<input name="zKeys" value="7" />
</div>

ASP.NET MVC Partial View with Form

I have a scenario I want to use a partial view but I'm having issues with it passing data to the controller. Here is a basic example of what I'm trying to do.
Objects:
Customer
Order
A Customer has an IList<Order> on it. I want the partial view to allow the user to edit the information. I can get the data to display but when the form posts the list under the Customer object is null.
I have also attempted to use a seperate form in my partial view. When I do this if I create paramenters on the controller like so I get the data:
public ActionResult UpdateOrders(IList<Guid> id, IList<int> quantity, IList<Guid> productId)
But when I do this
public ActionResult UpdateOrders(IList<Order> orders)
The list is null.
If anyone has a better suggestion of how to achieve this let me know.
How are you referencing the fields in your view? I'm thinking that it should be something like:
<input type="hidden" name="orders.Index" value="0" />
<input type="hidden" name="oders[0].ID" value="1" />
<input type="hidden" name="orders[0].productId" value="4" />
<input type="text" name="orders[0].quantity" value="6" />
<input type="hidden" name="orders.Index" value="1" />
<input type="hidden" name="orders[1].ID" value="2" />
<input type="hidden" name="orders[1].productId" value="2" />
<input type="text" name="orders[1].quantity" value="15" />
See Phil Haack's blog entry on binding to a list for more info.

Resources