I am currently attempting to submit a Form while adding extra information from a propagated model. Like my model already has information, and I wish to pass that along with the Form submission.
E.g. TestModel has Name, Date, and ReadTime properties.
In the View:
#using(Html.BeginForm(FormMethod.Post)){
#Html.TextBoxFor( model => model.Name, "Enter Name");
}
Notice how my form only submits the name, but I would like the Date to be submitted as well, which is propagated through from some other view. How would I achieve this?
EDIT: Assume that DATE is passed down into the View when the View inherited the model. Like Date already comes with the Model, and I need to pass it through with the form.
EDIT2: Date comes from the upper View hierarchy which calls this Partial View, which subsequently submits a form. I need the Date, which comes from upper view, to be submitted as a part of the form.
Just use a hidden input for Date like this:
#using(Html.BeginForm()) {
#Html.HiddenFor(model => model.Date)
#Html.TextBoxFor(model => model.Name, "Enter Name");
}
Related
I am using number format internationalization for input field.
e.g.my input field will take input values as 12,555.8 but when I submit form, I want to use value 12555.8 without comma.
I am using library react-number-format which is taking care of both the formats.
My question is how can I attach the 12555.8 number without comma to input field so that when I submit form, I can use that value?
<NumberFormat
thousandSeparator={this.props.thousandSeparator}
decimalSeparator={this.props.decimalSeparator}
value={this.state.formattedValue}
valOriginal={this.modifiedInputValue}
onKeyUp={(event) => {this.onKeyUpFormatted(event)}}
onValueChange={(values) => {
const {formattedValue, floatValue} = values;
this.modifiedInputValue = isNaN(floatValue)?'':floatValue;
this.setState({formattedValue});
}}
/>
I tried this code and while form submit tried to capture non formatted value like following
event.target.elements.valOriginal
I added property as data-valOriginal for React element. After this, we can access this value as element.dataset.valOriginal during submit form event.
I am using library react-number-format which is taking care of both the formats. My question is how can I attach the 12555.8 number without comma to input field so that when I submit form, I can use that value?
You do NOT want to attach the value of 12555.8 without the comma to the input field. Whenever you modify the "value" of an input field, you will be changing what is displayed for the user.
Instead, the solution is to do all data-modification outside of the form, right before it is submitted.
So on your form, you'll want to have an "onSubmit" handler, and in your react component, you'll define:
onSubmitHandler = (formData) => {
let cleanFormData = {...formData}
cleanFormData.myNumberInput = cleaningFunction(cleanFormData.myNumberInput)
//submit cleanFormData to server
}
There is a button on a website at the end of a form that I cannot seem to click with WWW::Mechanize. Here is the bit of HTML pertaining to this button:
<input type="submit" class="saveButton" value="Login">
When I print $mech->find_all_inputs();, I get return this:
HTML::Form::TextInput=HASH(0x7f8f52cdc450)
HTML::Form::TextInput=HASH(0x7f8f5302b488)
HTML::Form::SubmitInput=HASH(0x7f8f52cdc108)
The third one is the one I want to click. I'm not exactly sure how to click this button even though I've found it. I tried click(field(n => 3)), I tried assigning a variable $submit to find_all_inputs(3), then click($submit);, and no matter what, this button is not clicked.
Can anyone guide me as to how to click this elusive button?
Edit (after question answered)
Interrogating the HTML form found I was actually entering the password for the login into the 'Forgot my Password' field of the form. Why this field was not coming up for $mech->find_all_inputs(), I don't know since "Login" was. Either way, clicking the button takes me to the next page. Thanks!
Since it is the first button in the form, you can write this
$mech->click_button( n => 1 )
or, since it's value attribute is Login, you can do this
$mech->click_button( value => 'Login' )
But since it is the only button in the form, just
$mech->click
should work fine
Did you try to select the appropriate form first, then call click? It says (my emphasis)
Has the effect of clicking a button on the current form.
Find which form on the page you need. Let's say it's form number 2.
# $ua is the User Agent (Mechanize object), at the appropriate page
$ua->form_number(2);
# fill the form ...
my $response = $ua->click();
or
$ua->submit_form(
form_number => 2,
# fields => { name => $value } # can fill it here as well
};
I find click to be perhaps more reliable overall.
To inspect the forms you can use my #forms = $ua->forms. To fill the form you can use select or set_fields, for example. See Form Methods and Field Methods. All this operates with HTML::Form objects so you can use its methods as well. For example, value_names and possible_values are handy.
If this doesn't help please give us more detail -- the web page in question would be ideal.
I know I'm not supposed to use the Viewbag and that I should build a menu using Html.DropDowlListFor() so that i can add attribute bound to the members of the model, BUT, that would involve a pretty extensive code rewrite....
I have a custom controller with a menu:
*.ASCX
<%: Html.DropDownList("CityIDs", new SelectList(ViewBag.cities, "Id", "Name"), "--Select--", new { style = "width:200px" })%>
<%= Html.ValidationMessage("CityIDs") %>
The List populates just fine and I can default to the top item to "--Select--"
The prbolem is that I want the validation error to occur on anything that is not from the viewbag.... how can I achieve this?
Validation for dropdown lists only ensures that something was posted. If you want to ensure that the value that was posted is actually one of a set of "allowed" values, then you'll have to manually do that in your post action:
var cityIds = db.Cities.Select(m => m.Id);
if (!cityIds.Contains(model.CityIDs))
{
ModelState.AddModelError("CityIDs", "You must select one of the available choices.");
}
if (ModelState.IsValid)
{
...
Two things:
Notice that I'm pulling the city ids straight from the database (the actual code you'd need here, of course, depends on your specific implementation). The important thing, though, is that ViewBag only survives a single request, so you can't look for them in ViewBag after posting.
Despite the pluralized name of CityIDs, using the DropDownList helper ensures that only a single selected value will exist. If this is actually supposed to be a multiselect, then you need to use ListBox instead, and update this conditional here to account for check for multiple values.
Populates the top item with Text = "--Select--" and Value = ""
which will post a blank value for CityIDs and cause an error in ModelState.
Friends,
I'm working on changes to an existing code base someone else created. They used Razor Html.TextBoxFor in many places. I need to add another textbox using the same model property in a second place on the same form:
#Html.TextBoxFor(model => model.EnrollmentToBatchDataContract.NameKey)
The problem I have is that Razor is rendering the same ID for both instances of this textbox. I need to enable and disable the textbox's independent of each other.
Any insight would be greatly appreciated.
#Html.TextBoxFor(model => model.EnrollmentToBatchDataContract.NameKey, new { #id = "new id" })
The second parameter changes the Html object values. Any property from the Html tag could be put between the braces and changed.
This does not work on EditorForModel if you need to do that aswell, last time I tried.
This is closely related to a question I posted yesterday regarding CheckBoxes.
Here is my setup:
I have a database directory, with a list of names, among other fields, as my model.
I have a search page where users can search this directory and select a name.
I have a form page that displays the name with a checkbox next to it which allows the user to decide if they want to include the name as a value in the submitted form.
A controller that handles the submitted form.
Goal:
What I would like to know is, how can I get the string value of the name that was selected in the directory to both display in the form page View, and also include this string in the value field of the CheckBox?
You cannot change what the value of a checked check box; check boxes always get posted with "on" as their values if checked, and MVC can use this to automatically map the posted values to booleans. However, if this list of names is dynamic, and each name is unique, you can generate the form with dynamically-named check boxes. Then, in the controller, you can inspect the raw form to see which names were checked. For example, in the view, your code could look something like this:
<form>
<% /* Some loop here */ { %>
<input type="checkbox" name="name_<%= Html.Encode(theName) %>" />
<label><%= Html.Encode(theName) %></label>
<% } %>
</form>
Then, in the controller:
foreach (string name in listOfAvailableNames)
{
if (Request.Form["name_" + name] == "on")
{
// Handle the name being selected
}
else
{
// Handle the name not being selected
}
}