Get posted values from dynamically added controls in ASP.NET MVC - 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" />

Related

Do MVC3 non-sequential hidden input indexes need to come first?

MVC3 non-sequential index hidden inputs for model binding..
<input type="hidden" name="Index" value="whatever" />
Does it matter if they go before, after, in the middle of the other related inputs to be posted?
Does it matter at all where they end up in the posted data?
For example, can they all be lumped together and it still works?
<input type="text" name="[A].Id" value="1" />
<input type="text" name="[B].Id" value="2" />
<input type="hidden" name="Index" value="A" />
<input type="hidden" name="Index" value="B" />
No, the order of your form fields does not matter, nore where they appear on the html page.
The most important factor for MVC3 is the name of the fields must match to the name of your controller/action parameter.
If you have two fields with the same name however, only one value will be returned into your action.
As long as the hidden fields are located inside of the form it should not matter the order in which they are placed. Please see code sample below. Notice how the hidden fields are put anywhere inside of the form.
#using (Html.BeginForm())
{
#Html.ValidationSummary(false, "Please correct the following errors")
#Html.HiddenFor(m => m.CoolStuffId)
#Html.Partial("_EditCoolStuff", Model)
<fieldset class="ui-grid-a">
<div class="ui-block-a"><a data-role="button" href="#Url.Action("ActionPlan", "Store", new { id = Model.StoreID })">Cancel</a></div>
<div class="ui-block-b"><button type="submit" data-theme="a">Submit</button></div>
</fieldset>
#Html.HiddenFor(m => m.TypeId)
}

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")" />

Lists as mvc controller method arguments?

I have 2 tables containing checkboxes in a MVC form. The names of the checkboxes are currently more or less random.
Can I name the items in any smart way so that I can retrieve them as two named lists as controller method arguments? Preferably if I could do with prefixing the names.
<div>
<input type="checkbox" name="xyz" />
<input type="checkbox" name="foo" />
<input type="checkbox" name="123" />
</div>
<div>
<input type="checkbox" name="bar" />
<input type="checkbox" name="456" />
<input type="checkbox" name="baz" />
</div>
Can I somehow get it as arguments, similar to this?
public ActionResult DoThis(BlablahViewModel model, string[] firstList, string[] secondList)
{
Currently I just check for their existence roughly like this:
Request.Form["xyz"].Contains("t")
Thanks!
You'll need to use the List Model Binding features of MVC:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
For a good guide of how this can all work together:
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

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