Does the DropDownList selected property ever change in html? - asp.net-mvc-3

I am using mvc3 and have a dropdownlist (using DropDownListFor helper). I set the selected value to my default value. When the user selects a different item, the selected property of that item is not marked as such, i.e. the default value still has the selected property in the rendered html. Is this normal behaviour? If i use jquery and get the ddl val property it is correctly set to the recently selected item...is this just how dropdownlists work? I've checked IE, FF, Chrome and IE doesnt even have a selected property next to the options...

Edit:
Infact ive just tested it with firefox 9 + . It does change the html source with the selected changing place.
<option selected="selected" value="2">(0002) </option>
<option value="3">(0003) </option>
Ensure you are viewing the source of the current(correct) page, and the correct option list. Control + U in Firefox or Chrome.
As you change the dropdownbox, behind the scenes the selected value will have changed. As you have observed. And so submitting etc will use the new selected value.
So its nothing to worry about anyway :)

Related

How to extract visible text from webpage with selenium webdriver using ruby

I'm working with Selenium Webdriver using ruby.
I have a dropdown and after each value/text selected inside it, I want to extract that value/text visible on webpage interface without html tags, because for that value selected, in div > option tag I do not have a specific attribute for it, to figure out which is the value, can I do that in selenium webdriver using ruby?
Edit : HTML code :
<select name="OwningAndUsingCarPanel.UseOfVehicle" id="OwningAndUsingCarPanel_UseOfVehicle" autocomplete="off">
<option value="0" selected="selected">Please select</option>
<option value="227">Social only</option>
<option value="228">Social including commuting to a single place of work</option>
<option value="229">Business use by you</option>
</select>
UPDATE - more details:
I've tried this method and it's working if you select previous some value, but in my case I have some bug in my dropdown values, something like this: have 5 dropdowns one after another, and each of them is depending on the previous dropdown, so if i begin to fill the form like this:
1). select value at first dropdown (take the text)
2). select at the second dropdown some value (take the text)
3). select at the third dropdown some value (take the text)
4). select at the fourth dropdown some value (take the text)
5). select at the fifth dropdown some value (take the text) (this dropdown is generated by the previous fourth dropdowns values) - now the second value of dropdown is changed on the interface webpage. (so here I cant put element.text for the second dropdwon because it extract the value I have selected point 2).
I want to check if after each selecting value the previous value remain the same on the webpage inteface, in my case I have some bug at this.
You can get the value of the textfield using attribute :
element['value']
For any other element you can get the text using :
element.text
You can just do element.text to get the display value of an element.

MVC6 Taghelper Select Control with Selected Item in indexed model

I am trying to set the selected value of a select control based on a model in an indexed situation. I have the following code -
<select asp-for="Items.Details[i].One"
asp-items="Utility.DropdownListItems(date, Model.Items.Details[i].One)">
</select>
I am setting the selected item in the Utility that returns the SelectList, and would expect that to set the selected item of the drop down, but that doesn't work. If I inspect the option elements, none have the selected tag.
I know there was an issue with indexed data in previous iterations of Mvc HtmlHelpers. Does anyone know if this was resolved in MVC6?
For both HTML and tag helpers, generated <option> elements are selected based on the expression value (Item.Details[i].One in your case). IsSelected from the select list matters only if the expression value is null i.e. in the Create case.
We have corrected a number of issues related to indexed data in MVC 6. Please file an issue at https://github.com/aspnet/Mvc/issues if something is still not working.

Tablesorter filter dropdown options with values different than their labels

Tablesorter automatically builds the the select element for a column filter based on the settings in filter_formatter. Is it possible for tablesorter to set the <option> value attribute to be different than the label in a dropdown <select> filter in tablesorter when using ajax? It seems that tablesorter sets both to be the same.
For example, I'd like to be able to have the following dropdown to filter on languages:
<select>
<option value="en">English</option>
<option value="fr">French</option>
</select>
Using ajax, the two-letter value is used to query the database, but the full language name is better to show users for understandability.
What you have written above should work. When getting the selected value using javascript, find the select element, (call it var selectBox), and then selectBox.value will be the 'value' of the selected element. So if 'English' is what's showing as chosen, selectBox.value will equal 'en'.

Knockout Validation evaluates immediately on load

I'm using MVC, Knockout, and Knockout Validation to validate my view model.
I'm running into an issue where the validation for view model properties are firing immediately upon loading. In other words, "This field is required" shows up next to my inputs before a user has attempted to change their values.
This problem is specifically happening with dropdown (select) controls.
I'm guessing that this is a problem that I've created by somehow unintentionally changing/accessing/mutating the observable in another part of my javascript code. However, I don't know how to track this down.
Is there a way that I can somehow subscribe or track the even that fires that causes validation in Knockout Validation? I just need to know why this is firing in the way it is. I'm pretty confident that the value of the isValid() function is consistently false.
Here's a sample of how my HTML is setup on page load, unmolested:
<select class="highlightable validationElement" name="obsstate" data-bind="value: standardAnswers.ans106_1.value" required="true">
<option value="">-- Select -- </option>
<option value="AK">AK</option>
<option value="AL">AL</option>
etc...
</select>
After applying the bindings for the viewmodel. Then for that viewmodel make showAllMessages as false
Example
YourViewmodelname.errors.showAllMessages(false);
Quoting KO page.... ( http://knockoutjs.com/documentation/options-binding.html )
KO will prefix the list of items with one that displays the text “Select an item…” and has the value undefined. So, if myChosenValue holds the value undefined (which observables do by default), then the dummy option will be selected. If the optionsCaption parameter is an observable, then the text of the initial item will update as the observable’s value changes.
So, I solved it by setting "undefined" when defining the property, see example below:
self.myProperty = ko.observable(undefined).extend({
required : {"Field Required"}
});
Hope this helps...
I figured out this issue on my own.
The problem exists between the razor engine templating the select options, and then later binding the value of the selected element to Knockout.
Despite the fact that there is no user-inputted value in the select box, the default value, the "--select--" actually contains a value. In my case it was an empty string. Therefore, when I applied the knockout bindings, my viewmodel property was "updated" with the empty string value, and therefore validation fired.
To get around this in my case I set the default value of my model to be an empty string. Therefore when the bindings are applied, there is no valueHasMutated event fired on the Knockout observable, and thus no validation.

The value in drop-down menu changes to default after confirming the selection

I am writing a Selenium script in Ruby (As a beginner), wherein I need to change the selection of dropdown menu.
Functionality to automate-
1.Change the drop-down selection from a default value. (This dropdown is in an iframe)
2.Press Save button.
3.Handle a popup that confirms the change.
4.Page reloads with the new selection.
I am able to change the selection on UI by using "select(locator, value)". I have already disabled Javascript to handle the popup.
Issue-
Now, when I press the 'Save' button, the page reloads with the default value that was set.
e.g- Dropdown menu has 4 options-
<option value="920">Apple</option>
<option selected="selected" value="1">Mango</option>
<option value="910">BlackBerry</option>
<option value="921">Orange</option>
Now, when I change the selection to Orange, and then press Save, the page reloads but only with option 2 selected.
Please suggest any solution/reason for this and how can it be avoided? I hope I am clear with the question.
The main reason that might be happening is because you disabled the javascript. The script usually fires the postback event which loads the page with the new selected element. You should enable the javascript and handle pop-up using switch_to_alert function and explicitly click the confirmation button like a user would.

Resources