HttpContext.Current.Request.Form checkbox - c#

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>

Related

Bind radiobuttons to two different fields with Angular

need some help on radio buttons. How can i bind 2 radio buttons that are for one particular question but use 2 different fields in the database. For example there is a question that has a Yes/No answer. The yes answer binds to one field, and the No answer to another, they don't bind to a single filed that has a true, false value.
<div class="radio-s">
<input type="radio" ng-value="true" data-name="use_plan"
data-value="true" ng-model="record.offer_plan" />
<label>Yes</label>
</div>
<div class="radio-s">
<input type="radio" ng-value="false" data-name="use_plan"
data-value="true" ng-model="record.offer_plan" />
<label>No</label>
</div>
Thanks in advance.
Attach the name attribute to both of your radio buttons so that only one can be selected at a time and also bind them to the respective fields from your DB
<div class="radio-s">
<input type="radio" name="planGroup" ng-value="true" data-name="use_plan"
data-value="true" ng-model="record.offer_plan_yes" />
<label>Yes</label>
</div>
<div class="radio-s">
<input type="radio" name="planGroup" ng-value="false" data-name="use_plan"
data-value="true" ng-model="record.offer_plan_no" />
<label>No</label>
</div>

How to avoid #Html.CheckBoxFor generating hidden field?

Using #Html.CheckBoxFor generates a hidden field. Is there a way I can avoid generating that ?
Why I want to do that ? I was provided design, which has some script or library used in it for visual display. It works fine if Html is in below format (Checkbox with Label):
<div>
<input type="checkbox" value="resources" id="resources" class="cb">
<label for="resources">
I need an offset facility
</label>
<i class="tooltip-icon" data-icon="repayments"></i>
</div>
But it do not work if there is a hidden field between Checkbox and Label:
<div>
<input id="HomeLoanLead_Redraw_flexibility_Value" class="cb" type="checkbox" value="true" name="HomeLoanLead.Redraw_flexibility_Value" data-val-required="The Redraw_flexibility_Value field is required." data-val="true">
<input type="hidden" value="false" name="HomeLoanLead.Redraw_flexibility_Value">
<label for="HomeLoanLead_Redraw_flexibility_Value"> I want to make extra repayments </label>
<i class="tooltip-icon" data-icon="loan"></i>
</div>
If I try using <input type=checkbox> I m afraid I will not get out of box model binding in post action.
Please help.

How to send data from one page to another page with querystring and input form?

I am working in C#(asp.net). I have two pages 'abc.aspx' and 'xyz.aspx'. I want to send data from 'abc.aspx' to 'xyz.aspx'. I am using this code.
In 'abc.aspx'
<form action='xyz.aspx?site=google&code=123' method='get'>
<input type='text' name='name1' />
<input type='submit' value='submit' />
</form>
Now, I want to access all three values (site,code and name1). But, in 'xyz.aspx', I got only one value i.e name1. How to get all three values.
You need to put the values into hidden <input /> elements and hard-code the values if you want to have them end up in the query string. You're correct in setting the method='get':
<form action='xyz.aspx' method='get'>
<input type='hidden' name='site' value='google' />
<input type='hidden' name='code' value='123' />
<input type='text' name='name1' />
<input type='submit' value='submit' />
</form>
I think this one is the best.
In abc.aspx
<form action="xyz.aspx?site=google" method="post">
<input type="text" name="name1" />
<input type="submit" value="Submit" />
</form>
In xyz.aspx, access the data like this..
string site = Request.QueryString["site"];
string name = Request.Form["name1"];
//Remaining code...

fill 2 form one field in common with C#

I need to fill two forms on the same website, my issue is the field because they have the same name, e.g. the password field in both forms is passed, so how can I fill both?
<form action="post.php" method="post">
<input type="hidden" name="name" value="value" />
<input type="text" name="image" />
<input type="password" name="pass" size="10" tabindex="7" accesskey="Q" value="">
</form>
<form action="post2.php" method="post">
<input type="hidden" name="name" value="value" />
<input type="text" name="image" />
<input type="password" name="pass" size="10" tabindex="4" accesskey="P" value="">
</form>
the code to fill fields I'm using is this:
WebBrowser1.Document.GetElementById("pass").SetAttribute("value", stringpassword);
thanks
The id attribute specifies a unique id for an HTML element.
The id must be unique within the HTML document.
The id attribute can be used by a JavaScript (via the HTML DOM) or by CSS to make changes or style the element with the specified id.
And if your specified website is not following the standard Html practices then you should write your own custom
code to access those controls and fill them straight away. You can use Regex Or Xpath for accomplishing this purpose.

Using HTML radiobutton arrays in ASP.NET MVC

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].

Categories