ASP.NET MVC Html.DropDownListFor Select value - asp.net-mvc

I have a little problem. I use the Html.DropDownListFor helper to render a dropdown list to the client.
But I can't set the selected value in dropdown list.
<%= Html.DropDownListFor(model => Model.CalculationClassCollection,
new SelectList(Model.CalculationClassCollection, "ID", "Name", 3 ),
new { id = "ddCalculationClass" })%>
Anybody can help me?

If the SelectList is not a member of my Model, I'd use <%= Html.DropDownList("listBoxID", new SelectList(Model.CalculationClassCollection, "ID", "Name", 3 ))%>

It is one of the overloaded methods. I think it is the method with a parameter something like default. Sorry I am not in front of dev PC right now so I can't tell you exactly which method it is.

Related

Razor mutliselect dropdown- Set values as selected in the multiselect dropdown in ASP.Net MVC

I need a way to set already selected values in a multi select dropdown in a edit view in a web application (ASP.NET, Mvc)
<div class="form-group">
<label>Choose Categories</label>
#Html.DropDownListFor(model => model.SelectedValues, new MultiSelectList(Models.GetCategoryList().OrderBy(c => c.Value), "Key", "Value"), "Select Category", new { #class = "chzn-select form-control", multiple = "multiple" })
</div>
this is view code, here I passed selected ids in to selected values array.(when I return values from DB , assign those in to a array then assign to here.
I need a edit view like this
please anyone suggest way to do this
Thank you.
To generate this view you can use select2 jquery plugin. Check out the following link.
https://select2.github.io/examples.html#multiple

How can I show default message if no data and selected value if data available in dropdownlist in MVC4

I am binding the dropdownlist with some data and display the selected data in dropdownlist also. That works fine with the following code.
#Html.DropDownListFor(modelItem => item.UniformCommentSelected,
new SelectList(item.UniformComment),
item.UniformCommentSelected)
Now I want to add default message "Select" for dropdownlist.
I also want to show the selected value in dropdownlist if any and "Select" if not.
If I put "Select" as third parameter in DropDownListFor then it display "Select" for all users. If I put "Select" as 4th parameter in dropdownlist then nothing happens.
Can any one help me?
Try to use the following code.
#Html.DropDownListFor(modelItem => item.UniformCommentSelected, new SelectList(item.UniformComment),item.UniformCommentSelected == null ? "---Select---" : item.UniformCommentSelected)
in my project i fill my dropdown list like this it's below
#Html.DropDownListFor(model => model.CountryId, new SelectList(Model.Countries, "ID", "Name"), "select", new { #ID = "ddlCountry", #class = "text", Style = "width: 150px;", onchange = "javascript:cascadingdropdown();" })
i think this will help you
I usually bind to a collection of SelectListItem's, and manually add the blank of "Please select one..." option to that collection. In the controller (or in the view right before rendering the droplist), I set .Selected = true for the selected option.

How to set the default selected value of Dropdown on ASP.NET MVC View page

I have a dropdown that i need to set the default selected value from the database.
Example:
<%= Html.DropDownList("state", " -- Select one -- ")%>
The above code binds records from the ViewData["specialty"] from my entity model.
And "Select One" is added on the top.
Now if i need my dropdown to set the default value to 'NY'. How can i do that?
Appreciate your responses.
Thanks
Pass in a SelectList in the ViewModel
ViewData["States"] = new SelectList(states, "State_ID", "Name", "State_ID");
then
<%= Html.DropDownList("State_ID", (IEnumerable<SelectListItem>)ViewData["States"])%>

Selectlist and dropdownlist

I'd like to create a dropdownlist from following structure
List<KeyValuePair<long, string>> sponsori =
new List<KeyValuePair<long, string>>();
Now I want the selectlist to have the pair's long as data value, the string as text value and the selected item, if I have only access to the long of the selected item.
THanks in advance.
In your action code
yourViewModel.Sponsori= new SelectList(sponsori, "Key", "Value")
In your view code
<%=Html.DropDownList("yourSelectid", Model.Sponsori) %>
ViewData["selectList"] = new SelectList(sponsori, "Key", "Value");
And then on the page:
<%= Html.DropDownList("selectList") %>
You can also check out Rendering a Form in ASP.NET MVC Using HTML Helpers for a similar example (and more documentation).

bind drop down?

in the aspx page i am getting this error while binding dropdown list
Unable to cast object of type
'System.Web.Mvc.SelectList' to type
'System.Collections.Generic.IList`1[System.Web.Mvc.SelectListItem]'.
I have written:
<p>
<label for="categoryId">Category:</label>
<%= Html.DropDownList("categoryId", (IList<SelectListItem>)ViewData["categoryId"])%>
<%= Html.ValidationMessage("categoryId", "*")%>
</p>
please tell me the correct way of writing.
thanks
ritz
Here is a nice example of exactly what you are trying to accomplish:
How to bind IList with MVC Dropdownlist box
It looks like you will have to add some code-behind code to build the compatible list type.
what is the code in the controller action that you use to generate viewdata["categoryId"], here is what i normally do in the action code:
ArrayList categoryList=New ArrayList;
foreach (category c In YourcategoryCollection)
{ categoryList.Add(New With {.Item = c.categoryName, .value = c.categoryID})
}
Viewdata("categoryId")=New SelectList(categoryList, "Value", "Item", itemToEdit.categoryID)}
and then in your view, you just need:
<%= Html.DropDownList("categoryId", ViewData["categoryId"])%>

Resources