I have a Webgrid with a column containing a dropdown:
#Html.DropDownList("abc", new SelectList(Model.holdTypes),#item.holdType,"")
Here Model.holdTypes is a String List. #item.holdType contains the text that needs to be selected.
You are using a wrong overload. You should use this version.
#Html.DropDownList("abc", new SelectList(Model.holdTypes,item.holdType))
While this will render the SELECT element with a selected option, It will generate a SELECT element with only the text, There will not be a value attribute for the option items. So if you are planning to submit the selected option value to a form, This approach might not be very useful. You can use another version for that use case.
#Html.DropDownList("abc",
new SelectList(Model.holdTypes.Select(v=>new SelectListItem { Value = v,
Text = v}),"Value","Text",item.holdType))
Related
I have a dropdownlist on a page for which I want to set a text so that whether or not user makes any selection, it should always be displayed to the user and not the selected value.
For example:
Here Select Language should always be visible to user even if I select any value from the list i.e. Option 1 or Option 2.
I guess it is more of a CSS task which I am not very much comfortable with.
If you are talking about a simple asp:DropDownList, you just need to insert an item in it, with an index value of 0 (to deal with server-side checks).
myDropDownList.Items.Insert(0, "Select");
dropdownlist1.Text="SomeText";
Dropdownlist need to have the data-source.
Here Select Language should always be visible to user even if I select
any value from the list i.e. Option 1 or Option 2.
Solution:
if your dropdownlist isn't autopost back enabled then
Maintain a hidden field where on change event of the dropdownlist place the selected value or selected index or selected text there in the hidden field to remember the selection.
lets says the id of your dropdownlist is sel and hidden field is hdSel, the jquery code for that look likes below
$('#sel').change(function(){
var val = $("#sel option:selected").text();
$('#hdSel').val(val);
$("#sel").prop('selectedIndex', 0);
}
);
example on Jsfiddle for your understanding
If your dropdownlist is autopostback enable then you can assign the selected value or selected text or selected index of the dropdownlist to the hidden field and reselect the first item again
Hope this help
I have dropdown and binding values from database and values like below.
Value Text
AP AndraPradesh
BL Bangalore
KL Kerala
AP ArunaChalPradesh
When I select ArunaChalPradesh By default AndraPradesh is selecting back again.
Value fields for a dropdown list should be unique. Since they are generally not displayed, I'd say it'd be a good idea to change the value fields to be an ID of some sort instead of abbreviations of the Text.
Since you are trying to set the selected value to AP, it is grabbing the first value from the dropdown list.
If you are sticking with the abbreviations, just change the 2nd AP to another unique value (maybe AC?)
I am creating a options list using knockout data-binding. Basically on my server side i have an enum
public enum CarType
{
Saloon = 1,
Hatch = 2,
Convertable = 3,
SUV = 4
}
This basically get created into a dictionary on the server side and is sent back to the client side.
So the above enum will return to the client side as an object with a key and value.
My viewmodel is like so:
this.carName= ko.observable();
this.carType = ko.observable();
My options binding is as below:
<select class="form-control" data-bind="options: $root.carTypes, optionsText: 'value', optionsValue: 'key', value: carType"></select>
So basically when i press the edit button this view will get rendered correctly first time round. But if i cancel the edit page and re-open it then it selects the first element in the options list.
Not sure if i am doing anything wrong or if knockout behaves differently with dictionarys?
The car type object returned is an array of dictionary elements example below
[{key="1", value="Saloon"}, {key="2", value="Hatch"} .......
I'm not sure if I understand your problem. Do you wish to avoid the first item being selected? If so, take a look at the optionsCaption in the docs: http://knockoutjs.com/documentation/options-binding.html#parameters
optionsCaption
Sometimes, you might not want to select any particular option by
default. But a single-select drop-down list usually starts with some
item selected, so how can you avoid preselecting something? The usual
solution is to prefix the list of options with a special dummy option
that just reads “Select an item” or “Please choose an option” or
similar, and have that one selected by default.
This easy to do: just add an additional parameter with name
optionsCaption, with its value being a string to display. For example:
<select data-bind='options: myOptions, optionsCaption: "Select an item...", value: myChosenValue'></select>
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.
I have a drop down with yes/no as data inside a rad grid. When the value of dropdown in row is changed, values of dropdown in other rows excluding this should be set with second item. This must be done with javascript.
You could open this link.
In addition, I would suggest you changing this line:
GridEditFormItem item = DropDownList1.NamingContainer as GridEditFormItem;
to
GridEditableItem item = DropDownList1.NamingContainer as GridEditableItem;
in order to support both edit and insert scenario.
it would require 2 steps
right click on the dropdown a pop up will appear then check the postback option
after that simply double click on the dropdownlist as a result function will get created in your filename.cs file now put the code for adding the option for the other dropdown list there and u r done
I'm trying to generate a dropdownlist in my view, like the one below except I want my option label ("Please select...") to have a value of -1 as blank is already occupied by another option. Is there anyway to do this without adding it to the select list in my controller or view model, or creating a new html helper?
<%: Html.DropDownListFor(model => model.stuff.Id, new SelectList(Model.stuff, "Id", "Name"), "Please select...")%>
Thanks
I want my option label ("Please select...") to have a value of -1
You cannot do this out of the box with the DropDownListFor helper. It will always use blank for this value. You have two possibilities:
Write a custom helper
Add it manually to the collection
Actually you have a third possibility which IMHO is the best and what I would recommend you:
Don't use blank as values. This should be reserved for the Please Select default value. It doesn't seem logical to use blank as the value of something that the user can actually select. You might also have problems with the validation.
Just use the Concat method to add a new item to the SelectList after you get your values from the database. Then set the value to 0 and text to "Please Select".
var selectList = new SelectList(types.Concat( new[]{new MediaType() {MediaTypeId = 0, Type = "Please select"}})
, "mediatypeid", "type");