How to get value from Drodownlist in C# - c#

GetCurrency(countrydetials.Country) gives me currency of that specific country. But in my scenario, I only have two currencies in my Currency dropdownlist. If the country matches currency is not available in currency dropdownlist then by default it should select Indian Rupee.
Controller code
CountryDetials countrydetials = new CountryDetials();
var model = GetCurrency(countrydetials.Country);
countrydetials.Currency = model.FirstOrDefault().Currency;
View
#Html.DropDownListFor(m => m.Currency, new List<SelectListItem>
{
new SelectListItem {Value = "INR", Text="Indian Rupee" },
new SelectListItem {Value = "NPR", Text = "Nepalese Rupee" },
},
new { #class = "form-control" })
#Html.DropDownListFor(m => m.Country, new List<SelectListItem>
{
new SelectListItem {Value = "IND", Text = "India" },
new SelectListItem {Value = "NEP", Text = "Nepal" },
new SelectListItem {Value = "SIN", Text = "Singapore" },
new SelectListItem {Value = "USA", Text = "USA" },
},
new { #class = "form-control"})

Related

How to use Grouping for DropDownListFor in mvc 4

I need to use a Grouping in mvc4. How can I do it with mvc 4 DropDownListFor controls?
Here is a example:
Here is my real code in .cshtml for DropDownListFor:
#Html.DropDownListFor(model => model.Category, new List<SelectListItem>{
new SelectListItem() {Text = "Sales/Marketing", Value="Sales/Marketing"},
new SelectListItem() {Text = "BPO/Call Center", Value="BPO/Call Center"},
new SelectListItem() {Text = "Receptionist/Front Office", Value="Receptionist/Front Office"},
new SelectListItem() {Text = "Cook", Value="Cook"},
new SelectListItem() {Text = "Aayah/Child Caretaker", Value="Aayah/Child Caretaker"},
new SelectListItem() {Text = "Gardener", Value="Gardener"},
new SelectListItem() {Text = "Security/Guard", Value="Security/Guard"},
new SelectListItem() {Text = "Construction/Laborer", Value="Construction/Laborer"},
new SelectListItem() {Text = "Garment Tailor/Textile", Value="Garment Tailor/Textile"},
new SelectListItem() {Text = "Office Helper", Value="Office Helper"},
new SelectListItem() {Text = "Maid who can Cook", Value="Maid who can Cook"},
new SelectListItem() {Text = "Data Entry/Back Office", Value=""}},
"-- Choose Category --", new { #id = "Country", #class = "form-control", #data_error = "Choose No. of Employees is required", #required = "required" })
Even I have try to install DropDownList Optgroup MVC 1.0.0 but its not able to install on VS2012
My application framework is 4
How can I do this in client side with DropDownListFor use grouping for category?
you dont need any nuget packages you can use grouping SelectListGroup with SelectListItem like this
#{
var group1 = new SelectListGroup() {Name = "German Cars"};
var group2 = new SelectListGroup() {Name = "Swedish Cars"};
}
#Html.DropDownListFor(model => model.Id, new List<SelectListItem>{
new SelectListItem() {Text = "Audi", Value="Audi",Group =group1},
new SelectListItem() {Text = "Mercedese", Value="Mercedese",Group =group1},
new SelectListItem() {Text = "Saab", Value="Saab",Group =group2},
new SelectListItem() {Text = "Volvo", Value="Volvo",Group =group2}},
"-- Choose Category --", new { #class = "form-control", #required = "required" })
Edit
or you can do it manually with looping like this
first create a class
public class DropdownData
{
public string Name { get; set; }
public string GroupName { get; set; }
public string Id { get; set; }
}
then in action
ViewBag.Dropdowndata= new List<DropdownData>()
{
new DropdownData(){GroupName = "German Cars",Id = "Audi",Name = "Audi"},
new DropdownData(){GroupName = "German Cars",Id = "Mercedese",Name = "Mercedese"},
new DropdownData(){GroupName = "Swedish Cars",Id = "Saab",Name = "Saab"},
new DropdownData(){GroupName = "Swedish Cars",Id = "Volvo",Name = "Volvo"},
};
you can also use ViewModel instead of Viewbag
and finally in view
<select name="Category" id="Country" class="form-control" data_error="Choose No. of Employees is required" required="required">
<option value="">-- Choose Category --</option>
#{
string group = null;
foreach (var groups in (List<DropdownData>) ViewBag.Dropdowndata)
{
if (group != groups.GroupName)
{
group = groups.GroupName;
<optgroup label="#group">
#foreach (var data in (List<DropdownData>) ViewBag.Dropdowndata)
{
if (group == data.GroupName)
{
<option value="#data.Id">#data.Name</option>
}
}
</optgroup>
}
}
}
</select>

MVC - Show list of strings in DropDownList

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" })

Selected not working in html.dropdownlist MVC

I have the below code
person.State is Nullable string
Controller:
ViewBag.States = new List<SelectListItem>
{
new SelectListItem {Value = "", Text = "Please select", Selected = string.IsNullOrEmpty(person.State)},
new SelectListItem {Value = "ACT", Text = "ACT", Selected = person.State.ToLower() == "act"},
new SelectListItem {Value = "NSW", Text = "NSW", Selected = person.State.ToLower() == "nsw"},
new SelectListItem {Value = "NT", Text = "NT", Selected = person.State.ToLower() == "nt"},
new SelectListItem {Value = "QLD", Text = "QLD", Selected = person.State.ToLower() == "qld"},
new SelectListItem {Value = "SA", Text = "SA", Selected = person.State.ToLower() == "sa"},
new SelectListItem {Value = "TAS", Text = "TAS", Selected = person.State.ToLower() == "tas"},
new SelectListItem {Value = "VIC", Text = "VIC", Selected = person.State.ToLower() == "vic"},
new SelectListItem {Value = "WA", Text = "WA", Selected = person.State.ToLower() == "wa"}
};
View:
#{
List<SelectListItem> states = ViewBag.States;
}
...
...
...
#Html.DropDownList("State", states, new { id = "State", #class = "roleSelect" })
Even though one SelectListItem is Selected=true, in the output it is showing Please select. When I inspect the element in html output, none of the option has Select attribute.
Any suggestion?

How to selected value true in dropdownlist MVC

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);

Selected value missed if use model in DropDownListFor()

I am having trouble this DropDownListFor()
I have test controller:
model.COUNTRYNAME = "Swizerland";
ViewBag.Selecter = new SelectList(new[]
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "Swizerland", Value = "Swizerland", Selected =true},
new SelectListItem { Text = "Russia", Value = "Russia" },
}, "Text", "Value", model.COUNTRYNAME);
in View
#Html.DropDownListFor(x => Model.COUNTRYNAME , (SelectList)ViewBag.Selecter)
The DropDownListFor does not have select value, it always select first value.
What is wrong?
If i use DropDownList
#Html.DropDownList("COUNTRYNAME" , (SelectList)ViewBag.Selecter)
It also don't work.
But if I use
#Html.DropDownListFor("AAAAAAA" , (SelectList)ViewBag.Selecter)
It's work fine and select 2nd value! What happens? I don't understand.
Thanks
Try like this:
model.COUNTRYNAME = "Swizeland";
ViewBag.Selecter = new[]
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "Swizeland", Value = "Swizeland" },
new SelectListItem { Text = "Russia", Value = "Russia" },
};
return View(model);
and in the view:
#Html.DropDownListFor(
x => x.COUNTRYNAME,
(IEnumerable<SelectListItem>)ViewBag.Selecter
)
but a better way is to use a view model:
model.SelectedCountry = "Swizeland";
model.Countries = new[]
{
new SelectListItem { Text = "USA", Value = "USA" },
new SelectListItem { Text = "Swizeland", Value = "Swizeland" },
new SelectListItem { Text = "Russia", Value = "Russia" },
};
return View(model);
and in the view:
#Html.DropDownListFor(x => x.SelectedCountry, Model.Countries)

Categories