How to avoid #Html.CheckBoxFor generating hidden field? - c#

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.

Related

Binding data to textbox, making it uneditable

I have a textbox which I would like bound with data from the logged in user, so that the user can't edit it. Would this need to be a label, or is there another way?
<p>Practice:
<input class="form-control" type="text" value="6666666 - Siya's Awesome world" readonly="readonly" id="txtName" />
</p>
Ps. I don't want the values entered as I did above. Your help is appreciated.
Use input like this :
<input readonly="readonly" disabled="disabled" class="text-box single-line readonly" id="field-id" name="field-name" />

How to access value of of input type="text" control of HTML5 in asp.net usercontrol + Umbraco

I am using ascx page. which I am injecting in UMBRACO.
following is my .ascx Code.
<form id="main-contact-form" name="contact-form" method="post" action="#">
<div class="form-group">
<input type="text" runat="server" name="name" id="txtName" class="form-control" placeholder="Name" required />
</div>
<button type="submit" class="btn-primary">Send Message</button>
</form>
I have try to get value of text box (txtName) from back end but I am not abe to access it.
I have tried following method to do so..(Where strtxtName is a string variable)
1) strtxtName = txtName.Value
2) strtxtName = Request.QueryString("txtName")
3) Request.Form.AllKeys -> return Nothing.
But not able to get value of text Box ,
I don't think Umbraco is making any issue.?

HttpContext.Current.Request.Form checkbox

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>

MVC Problem with simple Postback

Bit of an odd one here:
The following razor syntax renders a nce simple html from with a submit button inside the bottom of it.
When i hit that button i would expect a postback to performed but for reason it dosn't ... any ideas??
Oh by the way this is the entire code for the view ...
#model FLM.PRL.EComms.Models.ReplySMS
#using (Html.BeginForm("Reply", "SMS", FormMethod.Post)) {
<h2>Follow Up</h2>
#Html.ValidationSummary(true)
#Html.HiddenFor(model => Model.From)
#Html.HiddenFor(model => Model.To)
<div class="editor-label">Reply</div>
<div class="editor-field">
#Html.EditorFor(model => Model.Message)
#Html.ValidationMessageFor(model => Model.Message)
</div>
<input type="submit" value="Reply" />
<br />
}
EDIT: Resulting markup generated by this view ...
<form action="/SMS/Reply" method="post">
<h2>Follow Up</h2>
<input data-val="true" data-val-required="The From field is required." id="From" name="From" type="hidden" value="xxxxxxxx" /><input data-val="true" data-val-required="The To field is required." id="To" name="To" type="hidden" value="xxxxxxx" /> <div class="editor-label">Reply</div>
<div class="editor-field">
<textarea class="text-box multi-line" id="Message" name="Message">
</textarea>
<span class="field-validation-valid" data-valmsg-for="Message" data-valmsg-replace="true"></span>
</div>
<input id="submitReply" type="submit" value="Reply" />
<br />
</form>
The only reason for this form not to be submitted are scripts interfering with the process. Maybe the validation scripts.
I noted that the Message field is required. Did you provided a value to this field? Don't you have any validation error message?
In the case validation is fine, I suggest removing selectively all the scripts from the page and see what happens. If removing a script results in your form working, you know it is the source of the problem.
I figured it out ... it's the hidden fields ... they are required but populated by the server during the postback so the auto validation prevents the postback client side before the server is able to populate the fields ...
solution:
Don't add empty required fields to an MVC form or turn off validation (not a good idea)

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.

Categories