I tried to follow this but the default modelbinder let my array null on the server side.
HTML:
Question 1:
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list[1]" type="radio" value="1002" />No
...
Controller action:
public ActionResult Anamnesis(string[] list)
{
If I choose only the second "No" (list[0] is missing) then the DefaultModelBinder is impossible to transform it into an array.
Thanx in advance!
Update#1
Reformatted based on the comment, thank you!
Update#2
Just a tought: created a hidden input after all list item, and in this way it works. But it's ugly, no doubt.
Question 1:
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
<input type="hidden" name="list[0]"/>
Question 2:
<input name="list[1]" type="radio" value="1002" />No
<input type="hidden" name="list[1]"/>
...
Order it's very important: the hidden value submits only when the radio is unchecked. The idea it's from the ASP.NET MVC helpers. (Btw I cannot use Html.RadioButton to archive this behavior.)
Your Update #2 seems like it solves your problem. Your Update #2 is also interesting in that you could also use this approach to supply a default value (such as 999) to be used whenever nothing is checked.
There is perhaps another similar way to do what you are asking, which is based on this article and which also uses hidden inputs. The idea is that you can create indexes for each of your radio sets, to avoid the situation where a missing selection earlier in the form causes all subsequent selections to be dropped:
Question 1:
<input name="list.Index" type="hidden" value="0" />
<input name="list[0]" type="radio" value="1000" />No
<input name="list[0]" type="radio" value="1001" />Yes
Question 2:
<input name="list.Index" type="hidden" value="1" />
<input name="list[1]" type="radio" value="1000" />No
<input name="list[1]" type="radio" value="1001" />Yes
The reason I suggest this, is in the case where you might like to associate your answers with a specific question by a unique ID, instead of just using 0, 1, 2 etc. The article I linked will show an example of how to do this.
Good luck!
-Mike
The name attribute of the radio button should be list, not list[n].
Related
I have umbraco v7.2.8
I have some template code like this
<input type="hidden" name="search" value=#Request.QueryString["search"]>
this works well to put the query string value for search string into the hidden field so when i click the submit on the surrounding form it requeries.
However, when there are spaces in the search string, Umbraco gets way to clever for itself and changes something like "red tree" to "red" tree=""
It is frustrating and seems to happen for fields as well - this must be a common enough problem. I can URLEncode it but then when I click the submit button it gets encoded again, which obviously isn't desirable, so I basically want the following to happen
QueryString?Search=red+tree
template: <input type="hidden" name="search" value=#Request.QueryString["search"]>
becomes: <input type="hidden" name="search" value="red tree">
NOT: <input type="hidden" name="search" value="red" tree="">
note that <input type="hidden" name="search" value=#HttpUtility.UrlEncode(Request.QueryString["search"])> gives <input type="hidden" name="search" value="red+tree"> which is again not what I need
This isn't Umbraco, it's the fact that you haven't enclosed your value in quotes. If you amend your code to:
<input type="hidden" name="search" value="#Request.QueryString["search"]">
It should work as you expect.
I am posting my form to an MVC Controller, where I want to process some changes the user has made on a grid like html structure.
I have checkboxes rendered as simple HTML in my View for each row :
<input type="checkbox" id="cbxR1" checked="checked" />
<input type="checkbox" id="cbxR2" checked="checked" />
I would like to know how I would retrieve a checkbox value after an AJAX post.
I am trying to use the following to retrieve the
HttpContext.Current.Request.Form["cbxR1"]
and am always getting a null, regardless of whether the checkbox was checked or not.
The cbxR1, cbxR2 should be the name of an input element, not the id.
<form ...>
<input type="checkbox" name="cbxR1" checked="checked" />
<input type="checkbox" name="cbxR2" checked="checked" />
</form>
I have a list of 100+ textboxes on my page. I want to have one textbox at the top that can change all of them to its value, yet still have the others able to be independent (as in, using one variable for all wouldn't work). They should be able to be changed individually, with the master one sort of acting as a "Change All".
My question is, would this work better by looping through and doing a postback in c#? Or can I dynamically change them all in jquery? Which would you recommend?
I would highly recommend changing them all with jquery. It could be as simple as something like this:
$('#txt_Master').change(function() {
$('.childTextBoxes').val($(this).val());
});
Using jQuery would be the best option.. As there is too much of load using the server side controls..
Check this FIDDLE
<input type="text" class="master"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
<input type="text" class="child"/>
$(document).ready(function() {
$('.master').on('change', function() {
$('.child').val( $(this).val() );
});
});
I have a form with four values. If any of them are empty I want to alert the user. Would the best way be to just have 4 separate if...then statements? or is there some sort of a fancy thing that I can do with C# to accomplish this?
thanks!
code
The form in question is a static HTML form:
<form id="form1" action="launch.aspx" method="post" target="_blank" name="form1">
<input type="hidden" name="ClientID" value="123456" />
<input type="hidden" name="Password" value="986574321" />
<input type="hidden" name="PracType" value="001" />
<input type="hidden" name="Encrypt" value="11258746345" />
</form>
Request.Form returns a NameValueCollection. You can loop through that collection and check if the value is set, if it's not then you can return the name of the missing field.
You can do this in C# on the .aspx.cs page, but I think Kev, is right. The RequiredFieldValidator is the quickest, simplest way to get the desired behavior you want.
By the way, why are all your form items hidden? Are we to assume you're using the control? If you can supply us with some more information, I think we can answer your question more accurately.
I have been trying out the NameValueDeserializer from MVCContrib, which will take a IList as a parameter to a controller and bind a form and its elements to it, but I was just wondering if MVC Beta had any way of doing this??
I know you can bind a strongly typed Object but I want to bind a List of these Objects for some bulk editing situations.
eg.
public void Save(IList<Item> items)
{
foreach (Item i in items)
{
//Save item
}
}
Is this possible in MVC Beta??
Yes it is, I wrote a detailed blog post about it here. It's really easy for simple types. For complex types, you'd need to do something like:
<input type="hidden" name="products.Index" value="0" />
<input type="text" name="products[0].Name" value="Beer" />
<input type="text" name="products[0].Price" value="7.32" />
<input type="hidden" name="products.Index" value="1" />
<input type="text" name="products[1].Name" value="Chips" />
<input type="text" name="products[1].Price" value="2.23" />
<input type="hidden" name="products.Index" value="2" />
<input type="text" name="products[2].Name" value="Salsa" />
<input type="text" name="products[2].Price" value="1.23" />