This question already has answers here:
Can the ViewBag name be the same as the Model property name in a DropDownList?
(2 answers)
Closed 5 years ago.
I have a drop down list on my view that for some reason doesn't display the selected value no matter what I've been trying, although I can use it to choose values and update the database with them with no problem.
VIEW
So on the view I have the following code to display the field
#Html.DropDownListFor(model => model.FormType, ViewBag.FormType as SelectList, new { #class = "form-control" })
The "ViewBag.FormType" is created in the controller from a SelectList object that i'm then passing in using a viewbag object.
CONTROLLER
I'm using some code to generate the selectlist and then choose the selected value from the value in the database for that record. It's here where i'm thinking that the problem is
short FormType = 0;
if (account?.FormType != null)
{
FormType = (short)account.FormType;
}
var FormTypeList = new SelectList(
new List<object>
{
new {value = "0", text = "Please Select..."},
new {value = "1", text = "Full"},
new {value = "2", text = "Short"}
}, "value", "text", FormType);
ViewBag.FormType = FormTypeList;
At first I thought that the problem was that the value in the database was a numerical value and the "value" in the selectlist was a text, so i've tried switching them both from one to the other and it's not helped. In the example that i'm working with the value in the database is 2.
I'm very lost to what the problem could be so any help would be appreciated.
#Html.DropDownList("FormType", null, "Select", htmlAttributes: new { #class = "form-control" })
Related
When I have an object with a value set, it is only shown initially if I leave out the "--Select--" parameter.
Any idea why that would be?
#Html.DropDownListFor(m => m.FlightDetails.AirlineId, ViewBag.AirlineList as SelectList, "--Select--", new { #class = "form-control selectpicker", #data_live_search = "true", #id = flight.Id + "AirlineId" })
Server-side:
You should set a default value for third params, look like this (Pay attention to SelectedDefaultValueHere)
var ViewBag.AirlineList = new SelectList(`listOfItemHere`,
"ValueOfDropdownItem",
"TextToDisplay",
SelectedDefaultValueHere);
Or You can set Selected of SelectListItem like this
new SelectListItem()
{
Value = "anyValue",
Text = "AnyText",
Selected = true
};
I found that this was my issue: https://stackoverflow.com/a/52431696/6591937.
I can't set the selected value when creating the SelectList because I'm using the same SelectList in more that one place and they all can have different values.
The way I resolved it in my case was that I passed in "--Select--" and I set the value via javascript (as I'm accessing it only via javascript anyway).
I am trying to use Html.DropDownListFor with the list of strings from a ViewBag.
Basically I got a ViewBag with a list of string like "Adam","Tom","Mike" etc...
I was wandering if it is possible to pass this list in to DropDownListFor where both Value and text are the items from my list.
I wasn't able to use foreach here...
example with hardcode:
#Html.DropDownListFor(model => model.Name, new SelectList(
new List<Object>{
new { value = "Adam" , text = "Adam" },
new { value = "Tom" , text = "Tom" },
new { value = "Mike" , text = "Mike" },
new { value = "Jake" , text = "Jake" },
},
"value",
"text",
2))
You can generate a collection of SelectListItems from the list of strings and use that with DropDownListFor helper method.
var names = new List<string> {"Adam", "Sam"};
ViewBag.Names = names.Select(f => new SelectListItem() {Value = f, Text = f});
and in the view
#Html.DropDownListFor(f=>f.Name, ViewBag.Names as IEnumerable<SelectListItem>,
"select one")
This question already has answers here:
How to set the default value for Html.DropDownListFor in MVC
(2 answers)
Closed 6 years ago.
I have this razor's statement in a view that create a dropdownlist.
How can I mantain the selected value after a post-back to the server ?
I set the selected value in a ViewBag property once on the server but I was not able to set the value again.
#Html.DropDownListFor(x => x.Languages, Model.Languages.Select(x => new SelectListItem { Text = x.Descr, Value = x.Code }), "Choose an option", new { htmlAttributes = new { #class = "form-control" } })
Thank you so much for the help.
When posting back to view in mvc just pass return view(model). model is the parameter you are catching on controller from view.
Update:
You can also do that view jquery if you have viewbag.Dropdown value like this:
#if (#Model !=null )
{
$("#DropdownId").val("#viewBag.DropdownListNameFromController")
}
Let's say my database has a reference table that maps the values Zebra, Goat, and Dog onto the keys 0, 1, and 2, and a DropDownList where a user can select one of these values.
#Html.DropDownListFor(
m => m.Animal,
new SelectList(
new List<Object>{
new {value = 0, text = "Zebra"},
new {value = 1, text = "Goat"},
new {value = 2, text = "Dog"}
},
"value",
"text"
),
new { #class = "form-control" }
)
After the user's selection is saved to the database, I'm trying to figure out a way to display the value for m => m.Animal. It's saved as a 2, but I want the displayed text to read Dog. How do I set the default selected value on the #Html.DropFownListFor equal to the user's selection (e.g. 2), so the selected item displays the text Dog?
I'm seeing two possible meanings to your question, so I guess I'll just cover them both.
If you're talking about making the drop down show the proper selected value, Razor should take care of this for you. However, the fact that you're defining the actual SelectList object, without specifying the selectedValue param, may be getting in the way. Technically DropDownListFor only needs IEnumerable<SelectListItem>, not an actual SelectList, and it's usually better to pass it the former as it's both easier and less prone to error:
#Html.DropDownListFor(
m => m.Animal,
new List<SelectListItem> {
new SelectListItem { Value = "0", Text = "Zebra" },
new SelectListItem { Value = "1", Text = "Goat" },
new SelectListItem { Value = "2", Text = "Dog" }
},
new { #class = "form-control" }
)
If, however, you're talking about how to translate the value 2 into Dog at some later point in your code, then you should actually employ something like an enum. For example:
public enum Animals
{
Zebra = 0,
Goat = 1,
Dog = 2
}
Then, change your Animal property to be of this type:
public Animals Animal { get; set; }
Finally, and since you're running MVC5, you can just use:
#Html.EnumDropDownListFor(m => m.Animal, new { #class = "form-control" })
You don't even need to pass a list of options because the helper will just get them from the enum the Animal property is typed to.
Then, when you want to display the selected animal type, you just use:
#Html.DisplayFor(m => m.Animal)
And Dog or whatever it's set to will be output automatically.
I'm building my first MVC application after years of doing webforms, and for some reason I am not able to make this work:
#Html.DropDownList("PriorityID", String.Empty, new {#class="textbox"} )
Error message:
System.Web.Mvc.HtmlHelper<SPDR.Models.Bug>' does not contain a definition for DropDownList and the best extension method overload System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, object) has some invalid arguments
Any help greatly appreciated!
Looking at the controller, and learing a bit more about how MVC actually works, I was able to make sense of this.
My view was one of the auto-generated ones, and contained this line of code:
#Html.DropDownList("PriorityID", string.Empty)
To add html attributes, I needed to do something like this:
#Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { #class="dropdown" })
Thanks again to #Laurent for your help, I realise the question wasn't as clear as it could have been...
UPDATE:
A better way of doing this would be to use DropDownListFor where possible, that way you don't rely on a magic string for the name attribute
#Html.DropDownListFor(x => x.PriorityID, (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { #class = "dropdown" })
As the signature from the error message implies, the second argument must be an IEnumerable, more specifically, an IEnumerable of SelectListItem. It is the list of choices. You can use the SelectList type, which is a IEnumerable of SelectListItem.
For a list with no choices:
#Html.DropDownList("PriorityID", new List<SelectListItem>(), new {#class="textbox"} )
For a list with a few choices:
#Html.DropDownList(
"PriorityID",
new List<SelectListItem>
{
new SelectListItem { Text = "High", Value = 1 },
new SelectListItem { Text = "Low", Value = 0 },
},
new {#class="textbox"})
Maybe this tutorial can be of help: How to create a DropDownList with ASP.NET MVC
If you are add more than argument ya dropdownlist in Asp.Net MVC.
When you Edit record or pass value in view bag.
Use this it will be work:-
#Html.DropDownList("CurrencyID",null,String.Empty, new { #class = "form-control-mandatory" })
There are some options in constructors look,
if you don't have dropdownList and you wanna insert CSS class you can use like
#Html.DropDownList("Country", null, "Choose-Category", new {#class="form-control"})
in this case Country is the name of your dropdown, null is for you aren't passing any generic list from your controller "Choose-Category" is selected item and last one in CSS class
if you don't wanna select any default option so simple replace "Choose-Category" with ""
You Can do it using jQuery
$("select").addClass("form-control")
here, Select is- html tag,
Form-control is- class name
#Html.DropDownList("SupplierId", "Select Supplier")
and here, SupplierId is ViewBagList,
Select Supplier is - Display Name
Simply Try this
#Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, new { #class="dropdown" })
But if you want a default value or no option value then you must have to try this one, because String.Empty will select that no value for you which will work as a -select- as default option
#Html.DropDownList("PriorityID", (IEnumerable<SelectListItem>)ViewBag.PriorityID, String.Empty, new { #class="dropdown" })
Try below code:
#Html.DropDownList("ProductTypeID",null,"",new { #class = "form-control"})
Try this:
#Html.DropDownList(
"country",
new[] {
new SelectListItem() { Value = "IN", Text = "India" },
new SelectListItem() { Value = "US", Text = "United States" }
},
"Country",
new { #class = "form-control",#selected = Model.Country}
)
You can simply do this:
#Html.DropDownList("PriorityID", null, new { #class="form-control"})
Try This
#Html.DropDownList("Id", null, new { #class = "ct-js-select ct-select-lg" })