This is my current razor code for a simple select.
#Html.DropDownList("Season", Model.Seasons.Select(s => new SelectListItem { Text = s.Name, Value = s.SeasonId.ToString() }), new { #class = "sel_season" })
What I want to do is add/insert an additional option item called "All".
I have tried Concat, but I can't seem to get it to work.
#Html.DropDownList("Season", Model.Seasons.Select(s => new SelectListItem { Text = s.Name, Value = s.SeasonId.ToString() }).Concat( new {"All", "0"}), new { #class = "sel_season" })
The following sample uses an overloaded extension method in the HtmlHelper class:
#Html.DropDownList("Season"
, Model.Seasons.Select(s => new SelectListItem {
Text = s.Name,
Value = s.SeasonId.ToString()
})
, "All"
, new { #class = "sel_season" }
)
Try This
#Html.DropDownList("Season",
Model.Seasons
.Select(s =>
new SelectListItem { Text = s.Name, Value = s.SeasonId.ToString() })
.ToList().Insert(0, SelectListItem { Text = "All",Value = "0"}),
new { #class = "sel_season" })
Related
I write this code to extract specific column to be added into the list, but its wrong. I want to:
List<Stone_Names> Stones = new List<Stone_Names>();
using (PSPlatformEntities1 p = new PSPlatformEntities1())
{
Stones.Add(new SelectListItem
{
Text = p.Stone_Names["stoneName"].ToString(),
Value = p.Stone_Names["nameID"]
});
}
It's very simple get the list from db first the bind into dropdown
ViewBag.DDlist= db.Stones.ToList().Select(x => new
SelectListItem()
{
Text = x.stoneName,
Value = x.nameID.ToString()
});
In view
#Html.DropDownListFor(model => model.modelProperty,
new SelectList(ViewBag.DDlist, "Value", "Text"),
"Select value", new { #class = "form-control" })
Note: stoneName , nameID should be your table name
I want to bind an html dropdown list with model data in such a way that “PWD-SUR” will display by default when “Account” = SBEN or SPPY or SPLF or SWOL
<div class="border col-md-2">
#Html.DropDownListFor(model => model.Account, new SelectList(new List<SelectListItem>
{ new SelectListItem { Text = "PWD-SUR", Value = "SBEN, SPPY, SPLF, SWOL"},
new SelectListItem { Text = "LOAN", Value = "LNPR"},
new SelectListItem { Text = "LN PYMT", Value = "LNIN"},
new SelectListItem { Text = "PREM", Value = "PREM"}
}, "Value", "Text"),"----", new {#id="ddlAccount", #class = "form-control" })
</div>
Is there any way I can achieve the same?
Note: model will contain only one account type at a time.
It works fine if I change the code like-
<div class="border col-md-2">
#Html.DropDownList("AccountType", new SelectList(new List<SelectListItem>
{ new SelectListItem { Text = "PWD-SUR", Value = "SBEN, SPPY, SPLF, SWOL" },
new SelectListItem { Text = "LOAN", Value = "LNPR"},
new SelectListItem { Text = "LN PYMT", Value = "LNIN"},
new SelectListItem { Text = "PREM", Value = "PREM"}
}, "Value", "Text", (Model.Account == "SBEN" || Model.Account == "SPPY" || Model.Account == "SPLF" || Model.Account == "SWOL") ? "SBEN, SPPY, SPLF, SWOL" : Model.Account), new { #id = "ddlAccount", #class = "form-control" })
</div>
According this article when you using SelectList, then you should specify SelectListItem value property which you want to set selected.
So you just need try like this:
<div class="border col-md-2">
#Html.DropDownListFor(model => model.Account, new SelectList(new List<SelectListItem>
{ new SelectListItem { Text = "PWD-SUR", Value = "SBEN, SPPY, SPLF, SWOL"},
new SelectListItem { Text = "LOAN", Value = "LNPR"},
new SelectListItem { Text = "LN PYMT", Value = "LNIN"},
new SelectListItem { Text = "PREM", Value = "PREM"}
}, "Value", "Text", (Model.Account == "SBEN" || Model.Account == "SPPY" || Model.Account == "SPLF" || Model.Account == "SWOL") ? "SBEN, SPPY, SPLF, SWOL" : Model.Account), new {#id="ddlAccount", #class = "form-control" })
</div>
I have a dropdownlistfor created with razor that displays 2 option : "show" or "hide" and they have a respective value of "0" and "1".
if (Model.Valeur == 0)
{
#Html.DropDownListFor(m => m.Valeur,
new List<SelectListItem> {
new SelectListItem { Value = "0" , Text = "Show", Selected = true },
new SelectListItem { Value = "1" , Text = "Hide" },
}, new { #class = "myselect" })
}
else
{
#Html.DropDownListFor(m => m.Valeur,
new List<SelectListItem> {
new SelectListItem { Value = "0" , Text = "Show" },
new SelectListItem { Value = "1" , Text = "Hide", Selected = true },
}, new { #class = "myselect" })
}
The if condition I made can set the right value on page load, but I was wondering if there was a way to set the selected value with a parameter or another option
Any Information would be gladly aprreciated.
You could use shorthand if to determine which option is selected.
#Html.DropDownListFor(m => m.Valeur,
new List<SelectListItem> {
new SelectListItem { Value = "0" , Text = "Show", Selected = Model.Valeur == 0 },
new SelectListItem { Value = "1" , Text = "Hide", Selected = Model.Valeur != 0 },
}, new { #class = "myselect" })
I have a DropDownListFor with a static selectList
Code:
#Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth,
new SelectList(Enumerable.Range(1, 12)),
(Model.FinancialInfo == null
|| Model.FinancialInfo.ExpiryDate == null)
? String.Empty
: null,
new { #class = "description-text" })
i need the first item is word Month How can i do that with this static DropDownListFor ?
I'm not sure what you checking in your DropDownListFor but i suggest you to do it this way:
#Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth,
//Genaration of month names
Enumerable.Range(1, 12).Select(x=> new SelectListItem
{
Value = x.ToString(),
Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x)
}),
"Month", //First Value if nothing selected
new { #class = "description-text" })
Finally Solved just to add the selected = true when the item equal the Model Value
#Html.DropDownListFor(model => model.FinancialInfo.FinancialExpiryMonth, Enumerable.Range(1, 12).Select(x => new SelectListItem
{
Value = x.ToString(),
Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x),
}), "Month", new { #class = "description-text" })
#Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles))
The above code creates me a select list just fine. But I want to make the selection optional. Unfortunately there is no empty option and I'd like to add one in. How would I do this?
By using the proper DropDownListFor overload:
#Html.DropDownListFor(
model => model.ZipFile,
new SelectList(ViewBag.ZipFiles),
"-- please select a zip file --"
)
#Html.DropDownListFor(model => model.Country, new List<SelectListItem>
{
new SelectListItem { Text = "India", Value = "1"},
new SelectListItem { Text = "USA", Value = "2"},
new SelectListItem { Text = "Sreelanka", Value = "3"},
new SelectListItem {Text = "Africa",Value="4"},
new SelectListItem { Text = "China", Value = "5" },
new SelectListItem { Text = "Austraila", Value = "6" },
new SelectListItem { Text = "UK", Value = "7" }
}, "Select Country",
new {#Style = "Width:500px;height:40px;",
#class = "form-control input-lg"})
In the controller, when you set ViewBag.ZipFiles, add a SelectListItem to that collection.