Setting the value of a textbox to a user entered value - textbox

How can I set the value of a textbox to user entered value.
For ex
<input type = "text" name = "somename" value = "">
If the user enters "3" then it should become
<input type = "text" name = "somename" value = "3">
All I want is that when I process the textbox for value in a servlet, I should be able to get the value 3 for that textbox. well, there is more to it as David says. The textbox is part of a cart application and it denotes quantity of an item. What I want to do is to be able to take the value of any item and store it in the database. I will have a default value of 1 for every textbox. But when I POST the data, I want it to post the value entered by the user and not the default value

The value in the textbox is automatically transferred to the server when you submit the form as somename=3 (or whatever value).
However, if you want to actually update the value attribute of the element, you could use jQuery to do something like:
HTML
<input id="name" type="text" name="somename" value="">
jQuery
$('#name').change(function(){
$(this).attr('value',$(this).val());
});
Javascript
(untested, but might work if you don't want to use jQuery)
<input type="text" name="somename" value="" onchange="this.attributes['value'] = this.value;">

Related

ASP.NET Core Tag Helper Checkbox does not save unchecked value

I'm adding a checkbox in an MVC form with Tag Helpers:
<div class="form-group">
<label asp-for="#Model.Billable"></label>
<input asp-for="#Model.Billable" type="checkbox" />
<input asp-for="#Model.Billable" type="hidden" />
</div>
When the user changes the value to Checked from Unchecked, the form value passed back to the controller is True - perfect.
However, if the value of the checkbox starts as Checked and the user changes it to Unchecked, the form value passed back to the controller is still True???
When the type is checkbox, the input tag helper creates a hidden element for the checkbox for you with the same name. If you check the view source of the page, you can see that
<input name="Billable" type="hidden" value="false">
When user enable/check the checkbox and submit the form, it will send 2 form data item, one for the actual check box with value True and the second for the hidden input with value False.
If you inspect the network traffic (Chrome devtools->network), you can see this
Billable: true
Billable: false
When model binder uses this data, it will give precedence to the True value and map that to your boolean property.
When user uncheck the checkbox and submit the form, it will send only one form data element, just the one for the hidden input element with value false and model binder will set false value for your boolean proeprty of your action method parameter.
Your current code is generating the checkbox element and an additional hidden input element with the same name Billable. So basically it will render 2 input elements(1 checkbox,1 hidden element which the helper created and 1 you explicitly created). When user unchecks the checkbox, It will send True for your explicitly created one and false for the one created by the tag helper.
If you inspect the network traffic (Chrome devtools->network), you can see this
Billable: True
Billable: false
Model binder will use this information and map the value True to your Billable property of your view model.
The solution is to remove the extra hidden element and rely on the one created by tag helper.
<div class="form-group">
<label asp-for="#Model.Billable"></label>
<input asp-for="#Model.Billable" type="checkbox" />
</div>

bind database value in html input type textbox in mvc

i need to know that how to bind value in html text box in mvc from database.
i am using datepicker in that input type textbox.
<input type="text" name="objmember.BirthDate" data-beatpicker="true" class="span4" />
i am able to submit the value from this textbox in database field but while edit it i need to already show the date in this input type text box.on the other hand other textbox are showing the value fine.
#Html.TextBoxFor(m => m.objmember.Name, new { placeholder = "Name", #class = "span4" })
i am using viewModel in this view.but writing name="objmember.BirthDate" is not binding the value with this textbox.
above default textbox of mvc works fine with it.but on using input type text it is not binding the value. i have to use this datepicker in input type field because it is not working with #Html.Textbox.
Please give me some solution to this problem.

TextBox value + TextBox value send one value

How can I concenate two texboxes in MVC, so when you click submit it will send values from two textboxes.
For example
<form method="get" action="#Url.Action("Index")">
#(Html.TextBox("q", Model.Search.FreeSearch))
??? //i need here another textbox
<input type="submit" value="Search"/> //when I click submit it will send values from both textboxess
</form>
Thanks for any idea...
If you add another field ( textbox) in the model and then pass this field from the model,
and create strongly typed view, then you can set two textboxes(fields) and send both value
and in the controller you can concate both field values.
e.g. model.firstname + model.lastname
hope this idea helps you.

MVC action on submit button press

I have a site that searches through a db and pages the results. I want to make it so whenever there is a new search the page index defaults to 1. To do this I want to pass a flag when the submit button is pressed to tell the controller to modify the page index. I thought you might be able to pass the value with the onclick attribute but haven't had any success.
Here is my code for the search text box and submit button (without my failed attempt at using the onclick attribute).
Find: #Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type = "submit" value = "Search"/ >
this answer is supplied to address your original question in order to not require major refactoring (see final comment for my suggestion). so, use a hidden input as such:
Find: #Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type="hidden" name="lastSearchValue" ViewBag.LastSearch as string />
<input type = "submit" value = "Search"/ >
then, when you POST the form, check to see if the value of lastSearchValue and SearchString are the same, if so do what you need to do. if i understand your reasoning, then the final step would be to set ViewBag.LastSearch to the value of the last set ViewBag.CurrentSearch.
A better solution would be to use a SearchViewModel to encapsulate all of this logic in a self contained way. This gives the benefit of not having disparate logic spread across different concerns.
You could use a hidden input:
Find: #Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type="hidden" name="resetPageIndex" value="true" />
<input type = "submit" value = "Search"/ >
The input value will get sent with the form submission. You'll have to update the POST parameter accepted by your controller accordingly.
You can pass the flag as a hidden input on the form:
#Html.Hidden("FlagValue", Your_Flag_Value);
Find: #Html.TextBox("SearchString", ViewBag.CurrentSearch as string)
<input type = "submit" value = "Search"/ >

How to mimic MVC's checkbox -> bool model binding?

I've got an editor template which renders out a checkbox:
#Html.CheckBoxFor(model => model.Follow)
Which renders something like this:
<input checked="checked" data-val="true" data-val-required="The Follow field is required." id="Follow" name="Follow" type="checkbox" value="true" />
<input name="Follow" type="hidden" value="false" />
AFAIK the hidden field is something to do with catering when an unchecked box isn't sent to the server or something.
Anyway, if i take a look at the Request.Form["Follow"] when the checkbox is checked, i see a value of "true,false".
How do i coerce a bool from this value? Do i simply ignore the second field? (e.g the hidden field).
I'm doing this is a base controller (protected method, invoked from child controller), so i don't have a strongly-typed view model, only the raw Request object.
Can anyone help? Or alternatively, if someone could point me to where in the MVC source code this happens, i could take a look myself, but not sure where to start looking.
You are correct the hidden field is just so the form will be submitted to the server. Because if the form had just checkboxes that are not checked then nothing will be submitted and the server would not know to set them to false.
You only require 1 hidden field per form, you do not need one per checkbox. But if your making your own control it is hard to tell if a hidden textbox is already on the field or not. If you know you are always going to have a textbox or select list etc somewhere else on your forms you do not need a hidden textbox at all
You can rename your hidden textbox to anything name it "dummy" or something different to the checkbox name so Request.Form["Follow"]; will only return the value of the check box not need to split. You never need to check the value of the "hidden textbox".
On a side note you shouldn't be using Request.Form["Follow"] you Action method should have a parameter like this instead "bool? follow"
MVC helper renders checkbox input control with two input fields, the checkbox and the hidden, because the browser do not send a value for checkbox input field if the checkbox is not selected. If you do not use auto mapping, you need to parse the input value that you receve from your form.
Use this simple rule to detect the checkbox:
var rawFollow = Request.Form["Follow"];
if (rawFollow.Contains("true"))
{
// do something
}
As far as i know, the extra hidden field is because if the checkbox is NOT checked, that input will not be submitted with the form and therefore we need the hidden field with the value of false.
So the only solution is can think of is this:
var rawFollow = Request.Form["Follow"];
var rawFollows = rawFollow.Split(',');
if (rawFollows.Count() > 1)
{
rawFollow = rawFollows[0];
}
But this seems hacky (and what about the order of the elements on the page, what if for some reason the hidden field was FIRST, then it would always evaluate to false), which is why i'm wondering how the MVC source does this.

Resources