In a SelectItem, I get back a collection of objects/values for use in a dropdown list. I need to find a specific item in the returned list and set the Selected property to true if it matches a value (stateCode) that is passed in... I have this which, obviously, doesn't work. The line after the //psuedo comment is where I'd like to make the change. Any idea on how to do this? I don't seem to have access to the properties I want to change...
SelectList retVal = new SelectList(this.db.States.Where(x => x.Col1== someVal), "ValueCol", "TextCol", someVal);
foreach (SelectListItem item in retVal) {
//psuedo
retVal[item.Index].Selected = item.Value == stateCode ? true : false;
}
return retVal;
As requested, here is how I'm rendering the dropdown that this SelectItem is bound to:
<div class="select" id="StateDropDown">
<label for="StateProvince">#Resources.Expert.State_or_Province:</label>
#Html.DropDownListFor(x => x.StateProvince, Model.StateList)
</div>
To assign a selected value to the drop down you don't need to set the value in your select list. all you need to do is set the value on your model when using a for helper. So for your drop down
#Html.DropDownListFor(x => x.StateProvince, Model.StateList)
make sure that you set StateProvince to the value that you want to select. Also make sure that the value of StateProvince Matches a value that is in your SelectList Model.StateList.
It would be appropriate to pass selected value to #Html.DropdownList method.
Assuming stateCode is state you want to be selected.
Like this.
<div class="select" id="StateDropDown">
<label for="StateProvince">#Resources.Expert.State_or_Province:</label>
#Html.DropDownListFor(x => x.StateProvince, Model.StateList, stateCode)
Related
I have a list of a certain object, each object contains fields. I need to do a DropDownListFor or DropDownList for that field within that object but its not binding
I initially tried this with a List and this works fine for binding to a normal object field. I then tried converting to SelectList() by doing
#Html.DropDownListFor(model => Model.MyListOfObjects[i].FieldName, new SelectList(Model.MySelectList, "Value", "Text"))
I then tried to set the "Selected" property on the SelectListItem I needed to be selected but still no luck
for (var i = 0; i < Model.MyListOfObjects.Count; i++){
#Html.DropDownListFor(model => Model.MyListOfObjects[i].FieldName, Model.MySelectList)
}
Model.MySelectList is List<SelectListItem>
public List<MyCustomObject> MyListOfObjects {get;set;} //populated
public class MyCustomObject{
public string FieldName {get;set;}
}
So in the end, I need the value of Model.MyListOfObjects[i].FieldName tp be selected from the Model.MySelectList(). The value in the select list does match so I cant see whats wrong
Thank you. Appreciate any help
EDIT: I know I can use "EditorFor" templates for the object used in Model.MyListOfObjects but I would prefer to avoid it if possible as I am using that object with other things so if I need another version of the object in editorfor then I wouldnt be able to do it. As a last resort though, I will just have to change the object to a unique one and go down the editorfor route
selectListItem contains text and value.
You need to bind value field to dropdown not text field.
i.e. model => Model.MyListOfObjects[i].FieldValue
Check This will help you
I have a model with a property "Countries" of type List. Country is an enum. The list can contain 0-5 countries. When I edit this model, I want to use a dropdownlist for each country in the list. I want to be able to select 0-5 countries for the property by using 5 dropdownlists. How do I do this? For properties with a single enum this works:
#Html.DropDownListFor(model => model.Country, (IEnumerable<SelectListItem>)ViewBag.CountryList)
The right country gets selected like it should. For my multi-country property this does not work:
#for (int i = 0; i < 5; i++)
{
#Html.DropDownListFor(model => model.Countries[i], (IEnumerable<SelectListItem>)ViewBag.CountryList)
}
That is, I get my dropdownlists, but the countries doesn't get selected like in the example with the single country.
In my controller I have:
ViewBag.CountryList = new SelectList(Enum.GetValues(typeof(Country)));
Any suggestions? I'm not very good at MVC.
You can use this in MVC5:
#Html.EnumDropDownListFor(m => m.Country)
The following block of code is used to make the selected value of a dropdownlistfor persist after submit:
#Html.DropDownListFor(x => Model.Value, new SelectList(Model.Values, #Model.Value))
In this block of code, Model.Values is a list of strings and the #Model.Value specifies which value is selected.
This block of code works great but I'm trying to do the same thing with a list of SelectListItems (rather than a list of strings). In the following code Model.Values is a list of SelectListItems.
#Html.DropDownListFor(x => Model.Value, Model.Values, Model.Value,
new
{
id = "ValueDD", onchange = "this.form.submit()"
})
Is it possible to use #Model.Value as I did in the first code block to set the selected value?
SelectListItem has a property Selected that needs to be set for the selected item, see http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlistitem.ASPX
So in your controler you can just set the Selected property of the item that needs to be selected, assuming you are using the controler to build the list of Model.Values
As I am new to MVC framework, I have been spending a couple of hours to generate a check box list in a View file. Finally, I could figure it out well. Here is my code:
#foreach (var item in Model.GetRoleNames)
{
#Html.CheckBox("chk_" + item.Value, new { value = item.Value })#item.Text<br />
}
But, I need to detect which of them is selected and if all the ckeckboxes are left unchecked, then preventing some operations.
Question: How can I get the checked items when I am within a controller action?
As the others said, you should use a Boolean value as the second parameter to CheckBox to indicate the checked status. A bit of string manipulation should help you get the ids of the selected check boxes..
First lets change the way the checkbox helper is used
<div id='myCheckboxDiv'>
#foreach (var item in Model.GetRoleNames)
{
#Html.CheckBox("chk_" + item.Value, false)#item.Text<br />
}
</div>
As you can see, I have set the second parameter to false and wrapped the mix in a div
And, when you want to get the ‘values’ associated with the selected checkboxes
var values = $(‘# myCheckboxDiv’).find('input:checkbox:checked').map(function () {
// get the name ..
var nameOfSelectedItem = this.attr('name');
// skip the ‘chk_’ part and give me the rest
return nameOfSelectedItem.substr(4);
}).get();
I am assuming item.Value to be a number. If its is not, please remove the white spaces using C#
I have a bunch of drop down lists in my view, such as this one:
#Html.DropDownListFor(model => model.Process, Model.ProcessList)
They all take in a SelectList based on data from a database table like this:
ProcessList = new SelectList(_db.Processes, "ID", "Name");
where _db.Processes returns a ObjectSet<Process>.
The issue I have is that sometimes the property that is set by the drop down list can be no selection, i.e. null. How can I add a null selection to the SelectList>
#Html.DropDownListFor(model => model.Process, Model.ProcessList,"--Select Process--")
The above line would add --select process-- at the top of select list and if this value is selected, empty string will be posted and bound property will be set to null (Process in this case)