I have a nullable bool in my model
public bool? Property { get; set; }
And I render it via EditorFor
#Html.EditorFor(model => model.Property)
How can I add class form-control to rendered select and how can I localize strings Not set, True and False? Or better how can I replace them with custom strings?
#Html.DropDownListFor(m => m.Property, new SelectList(new[]
{
new SelectListItem { Value = null, Text = "Not set" },
new SelectListItem { Value = false, Text = "False" },
new SelectListItem { Value = true, Text = "True" },
},
"Value",
"Text"
),
new { #class = "form-control" })
You can try with below code:
#Html.DropDownListFor(m => m.Property,
new List<SelectListItem>(){
new SelectListItem { Value = "False", Text = "False" },
new SelectListItem { Value = "True", Text = "True" }
},"Not set",new { #class = "form-control" })
Related
Below is the dropdownlist of Country. I want selected text "Select" which is working.
#Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
new { #class = "form-control" })
Now I want to set the value "0" for text "Select" which is by default selected. Currently the value for "Select" is blank as shown below.
How can I do this? The value "Select" is not in the data source. I have to access this selected value in JQuery.
I have tried with these two but none of those are working.
#Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
"Select", new { #class = "form-control", #selected = "0" })
And
#Html.DropDownList("ddlCountryName",
new SelectList(ViewBag.CountryName, "CountryId", "CountryName"),
"Select", new { #class = "form-control", #selected = "0" })
Below is the controller code for CountryName values
ViewBag.CountryName = dbLMS.CountryMasters.Select(c => new { c.CountryId, c.CountryName }).OrderBy(c => c.CountryName).ToList();
You can do as follows:
Option-1:
#{
var countrySelectList = new SelectList(ViewBag.CountryName, "CountryId", "CountryName");
List<SelectListItem> countrySelectListItems = countrySelectList.ToList();
countrySelectListItems.Insert(0, (new SelectListItem { Text = "Please select", Value = "0", Selected = true }));
}
#Html.DropDownList("ddlCountryName", countrySelectListItems , new { #class = "form-control" })
Option-2:
In the controller method:
List<SelectListItem> selectListItems = dbLMS.CountryMasters.Select(a => new SelectListItem()
{
Text = a.CountryName,
Value = a.CountryId
}).ToList();
selectListItems.Insert(0, new SelectListItem(){Text = "Selet Country", Value = "0", Selected = true});
ViewBag.CountrySelectList = selectListItems;
Then in the view:
#Html.DropDownList("ddlCountryName", (List<SelectListItem>)ViewBag.CountrySelectList, new { #class = "form-control" })
new SelectList(ViewBag.CountryName, "CountryId", "CountryName", //Default value)
You dont really need a value for Select. For the model, keep the Required validation attribute.
This will ensure the value is selected. This way you dont have to check in the backend.
Based on this code (auto-generated Control/View code from Visual Studio):
<div class="form-group">
#Html.LabelFor(model => model.Type, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#*Comment: Want to replace EditorFor to DropDownList("Banana, "Apple", "Orange"*#
#Html.EditorFor(model => model.Type, new { htmlAttributes = new { #class = "form-control" } })*#
#Html.ValidationMessageFor(model => model.Type, "", new { #class = "text-danger" })
</div>
</div>
I would like to add something similar like:
#Html.DropDownList("UserId", null, htmlAttributes: new { #class = "form-control" })
(also auto-generated code, but for UserId) instead of #html.EditorFor...
I was able to see list of UserName(value = Id) from db via Controller:
ViewBag.UserId = new SelectList(db.Users, "Id", "UserName", Test.UserId);
Now, I don't want that ViewBag read from database, instead I want it to list 3 different strings that I will use to let user to chose for indata(meaning, limit them to choice one of these 3 string instead writing it).
Strings I want it to list:
"Banana"
"Apple"
"Orange"
How do I do that?
Try this,
#Html.DropDownList("DropDown", new List<SelectListItem>
{ new SelectListItem { Text = "Banana", Value = "1", Selected=true},
new SelectListItem { Text = "Apple", Value = "2"},
new SelectListItem { Text = "Orange", Value = "3"}
}, "Select Fruit")
OR
Get value in model
#Html.DropDownListFor(x => x.Id, new List<SelectListItem>
{ new SelectListItem { Text = "Banana", Value = "1", Selected=true},
new SelectListItem { Text = "Apple", Value = "2"},
new SelectListItem { Text = "Orange", Value = "3"}
}, "Select Fruit")
Simply change what you are assigning to ViewBag.UserId f.e. like this:
var fruits = new List<string> { "Banana", "Apple", "Orange" };
ViewBag.Fruits = fruits.Select(f => new SelectListItem { Text = f, Value = f });
This is how you can build your list items:
ViewBag.Fruits = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Selected = true, Text = string.Empty, Value = "-1"},
new SelectListItem { Selected = false, Text = "Banana", Value = 0},
new SelectListItem { Selected = false, Text = "Apple", Value = 1},
}, "Value" , "Text", 1);
Here is another way of doing it using enum
public enum Fruit { Banana, Apple, Orange }
#Html.DropDownList("FruitSelection",
new SelectList(Enum.GetValues(typeof(Fruit))),
"Select Fruit",
new { #class = "form-control" })
MVC Controler
ViewBag.PageSort = new List <SelectListItem> ()
{
new SelectListItem () {Value = "1", Text = "Discounts"},
new SelectListItem () {Value = "2", Text = "New Items"},
new SelectListItem () {Value = "3", Text = "Lowest Price"},
new SelectListItem () {Value = "4", Text = "Highest Price"}
};
in View
#Html.DropDownList("PageSort", ViewBag.PageSortas SelectList, "- Select Sort -", new { #id = "SortID", #name = "SortID", #class = "form-control" })
I have this code that can retrieve a list from database in dropdownlist. I want to add hardcoded value or option to this.
Controller:
IEnumerable<SelectListItem> items = db.specimentype.Where(model => model.recordstatus == "active").ToList().Select(c => new SelectListItem
{
Value = c.code.ToString(),
Text = c.specimenType
});
ViewBag.specimentypelist = items;
View:
#Html.DropDownListFor(model => model.SpecimenType, new SelectList(ViewBag.specimentypelist, "Value", "Text", Model.SpecimenType), new { #class = "specimendropdown", #style = "height:27px; margin-left: 40px;" })
I want this to be
#Html.DropDownListFor(model => model.SpecimenType, new List<SelectListItem> { Text = "Horizontal", Value = "Horizontal" }, new SelectList(ViewBag.specimentypelist, "Value", "Text", Model.SpecimenType), new { #class = "specimendropdown", #style = "height:27px; margin-left: 40px;" })
You should move your list generation to the controller, like so for instance
model.SelectListOptions= new List<SelectListItem>
{ new SelectListItem() { Text = "DatabaseOption1", Selected = true, Value = "1"}
, new SelectListItem() { Text = "DatabaseOption2", Selected = false, Value = "2"}
, new SelectListItem() { Text = "AddedOption1", Selected = false, Value = "3"} };
And in your view, you can now reference it like so:
#Html.DropDownListFor(x => x.SpecimenType, Model.SelectListOptions, null, new { #class = "specimendropdown", #style = "height:27px; margin-left: 40px;", #disabled = "disabled" })
UPDATE
To account for your database retrieval, there's not much you need to do. You could add the SelectListItems to the list, like so:
IEnumerable<SelectListItem> items = db.specimentype.Where(model => model.recordstatus == "active").ToList().Select(c => new SelectListItem
{
Value = c.code.ToString(),
Text = c.specimenType
}).ToList();
items.Add(new SelectListItem() { Text = "AddedOption1", Selected = false, Value = "3"});
ViewBag.specimentypelist = items;
Just convert the IEnumerable to a List, and use the Add() method to append your options to the list you retrieve from the database.
You can also use the Insert method if you want to put them in at a specific place.
I use MVC and have a property with bool?.
I want to use a dropdown box for this, so my Model has a List like this:
var YesOrNoTriState = new List<SelectListItem> {
new SelectListItem { Text = "Select", Value = "" },
new SelectListItem { Text = "Yes", Value = true.ToString() },
new SelectListItem { Text = "No", Value = false.ToString() }
};
My model is null, so it has no value but true will be selected...
Here is my Razor Code:
#Html.DropDownListFor(
x => x.MoProVerglasung,
Model.MoProVerglasungDropDown,
new { #class = "form-control" })
i have problem in dropdownlist selected value.
View
#Html.DropDownList("QuestionType", (IEnumerable<SelectListItem>)ViewData["questiontype"], new { id = "QuestionType", #style = "max-width: 200px;width: 200px", #OnChange = "HideForm(this)", #class = "form-control" })
Controller
var questionTypeList = new SelectList(
new List<SelectListItem>
{
new SelectListItem { Value = "1", Text = "Automatic", Selected = false },
new SelectListItem { Value = "2", Text = "Custom", Selected = true}
}, "Value", "Text"
).ToList();
var questionType = new SelectList(questionTypeList, "Value", "Text");
ViewData["questiontype"] = questionType;
i want value=2 is selected true in view but my script above is not working, how can i solve this problem?
i have modified my code. i'm not using stronglytype which bind to dropdownlist. i'm using viewdata. this is my code:
Controller
List<SelectListItem> questionTypeList = new List<SelectListItem>();
questionTypeList.Add(new SelectListItem() { Text = "Automatic", Value = "1" });
questionTypeList.Add(new SelectListItem() { Selected = true, Text = "Custom", Value = "2" });
ViewData["questiontype"] = questionTypeList;
View
#Html.DropDownList("QuestionType", ViewData["questiontype"] as SelectList, new { id = "QuestionType", #style = "max-width: 200px;width: 200px", #OnChange = "HideForm(this)", #class = "form-control" })
why this code work and why previous code not work? i'm so confuse...
Not sure if you have a strongly typed view but you can use
DropdownlistFor(model=>model.QuestionType, selectlistItem)
then in your controller set
model.QuestionType to 2;
then
return View(model);