Razor DropDownFor default text and value - asp.net-mvc

I have created a dropdown list in razor as,
#Html.DropDownListFor(model => model.ItemID, new SelectList(Model.ItemList, "Id", "ItemCodes", String.Empty), "ALL", new { #id = "ItemDetails"})
Here I wanted a default value which I added as 'All'. The text 'All' will be the first element, which is fine. However how can I make sure that the All has the value as '0'.
That is current Html markup is <option value="">ALL</option>.
I needed that as <option value="0">ALL</option>.
How can I add that as part of above Razor declaration.

You can not do this by helper syntax instead of this You canto insert an item at 0 index like this.
Model.ItemList.Insert(0,new SelectListItem{Value="0",Text="ALL"});

Use Jquery to do this:
$("#ItemDetails").prepend("<option selected=selected value='0'>All</option>");
and ur htmlhelper will be as:
#Html.DropDownListFor(model => model.ItemID, new SelectList(Model.ItemList, "Id", "ItemCodes", String.Empty), new { #id = "ItemDetails"})

Related

ASP.NET MVC Select Option Value of SelectList

I am rendering a dropdown list.
ViewBag.Categories = new SelectList(db.HelpCategories, "ID", "Name");
In my view I call it like this:
Categories: #Html.DropDownList("Categories", (SelectList)ViewBag.Categories, "All")
For which I get select values like this.
<select id="Categories" name="Categories">
<option value="">All</option>
<option value="1">Dėl Krepšelių</option>
</select>
I really need to set the value of "All" to 0. Can't figure out a way to do it. Found a tutorial that it's done with SelectListItem, but that doesn't fit my code.
Any help is really appreciated, thank you.
You can always build the SelectListItem list in your action method where you can add an item with the specific value and text explicitly, if you absolutely want that.
var categoryList = new List<SelectListItem>
{
new SelectListItem { Value="0", Text = "All"}
};
categoryList.AddRange(db.HelpCategories
.Select(a => new SelectListItem {
Value = a.Id.ToString(),
Text = a.Name}));
ViewBag.CategoryList = categoryList;
return View();
Now in your view, you will use ViewBag.CategoryList to build the SELECT element
#Html.DropDownList("Categories", ViewBag.CategoryList as IEnumerable<SelectListItem>)

Razor #Html.DropDownList() do not take the selected value from List<SelectListItem> selected value

In my controller a have this:
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = row["ProgramName"].ToString().Trim(),
Value = row["Id"].ToString().Trim(),
Selected = row["selected"].ToBoolean()
});
but when I send the object to the razor view I get it like this
#Html.DropDownList("ListaProgramas", (IEnumerable<SelectListItem>)ViewData["ListaProgramas"], new {#class = "form-control pull-left", #id = "ListaProgramas", #style = "color:black; width:100%;" })
That look like this:
but Razor not set selected value. What i would do for resolve this issue ?
The HTML is this
<select class="form-control pull-left" id="ListaProgramas" name="ListaProgramas" style="color:black; width:100%;">
<option selected="selected" value="1">VIADR</option>
<option value="2">TweakedBPclient</option>
</select>
But in my controller I selected a different one
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
{
Text = row["ProgramName"].ToString().Trim(),
Value = row["Id"].ToString().Trim(),
Selected = valor
});
I want HTML Razor view take as selected the element that I selected in the controller code.
When crafting your SelectList, use this form instead:
new SelectList(ViewData["ListaProgramas"], "Value" , "Text", itemToSelect);
Where itemToSelect is the appropriate row["Id"] - since you don't have a model in play, you can pass this value in via the ViewData, as well. That being said, I also advise using strongly-typed views and helpers and simply setting the value in the model that is passed to the view, thereby cutting out the need to specify the itemToSelect parameter altogether.

Razor syntax for DropDownListFor

How can I write code below with a HTML helper?
<select name="CountryId">
#foreach (var c in ViewBag.Counties) {
<option value="#c.Id">#c.Name</option>
}
</select>
This code above will give the right html code in my browser. But if I use the HTML helper below the value attribute from the option tags are missing.
#Html.DropDownListFor(
model => model.CountryId,
new SelectList(ViewBag.Countries, "Name"),
new { htmlAttributes = new { #class = "form-control" } }
)
What's wrong in this code if you know that ViewBag.Counties is an object from type List<Country> and has the properties Name (type of string) and CounrtyId (type of int).
Try this.your selectlist item class should be created in the parent class that is model class you are binding in the view page.
#Html.DropDownListFor(model => model.CountryId,new SelectList(ViewBag.Countries,"CountryID", "CountryName"),new {#class='form-control'})

How to Create an Empty Html.DropDownlistFor & how to disable Html.TextBox

I have the following code inside my asp.net mvc view:-
#Html.DropDownListFor(model => model.FirewallCustomer.CustomerVLANSID,null, null, null)
#Html.ValidationMessageFor(model => model.FirewallCustomer.CustomerName)
<div><span class="f">VLAN IP</span> #Html.TextBox("VLANIP", new { disabled = "disabled"})</div>
but the above code will raise an error on the DropDownListFor:-
There is no ViewData item of type 'IEnumerable' that
has the key 'FirewallCustomer.CustomerVLANSID'.
and for the TexBox it will show the disabled = "disabled" inside the textbox body, instead of disabling it. can anyone adivce on how to solve my dropdownlist & my textbox problems???
Thanks`
For textbox
#Html.TextBox("VLANIP", new { disabled = "disabled"}) change to
#Html.TextBox("VLANIP","Your Value", new { disabled = "disabled"}) . I would like to advise you change disabled = "disabled" to #readonly = "readonly".
For dropdown list, if you wanna create a empty dropdownList for, you have to convert you FirewallCustomer.CustomerVLANSID to SelectListItem and add empty value for it. Please post your action for this view, i can help you. Should try
Html.DropDownListFor(model => model.FirewallCustomer.CustomerVLANSID,Enumerable.Empty< SelectListItem >(),null)

MVC3 Razor SelectedValue in DropdownListFor

I've the following code for a DropDownListFor and is working ok
#Html.DropDownListFor(Function(model) model.Habilitacoes, New SelectList(ViewBag.Habilitacoes, "KeyHL", "DescricaoHL"), New With {.class = "FillHSpace"})
The ViewBag.Habilitacoes is a List(Of T)
But know I want to add the SelectedValue to the DropDownListFor, so I've used the following code but doesn't work.
#Html.DropDownListFor(Function(model) model.Habilitacoes, New SelectList(ViewBag.Habilitacoes, "KeyHL", "DescricaoHL", model.Habilitacoes), New With {.class = "FillHSpace"})
How can I declare the SelectedValue?
You should not use the same property as first and second argument of the DropDownListFor helper. The first argument is a scalar property and the second a collection:
#Html.DropDownListFor(Function(model) model.SelectedHabilitacoes, New SelectList(ViewBag.Habilitacoes, "KeyHL", "DescricaoHL"), New With {.class = "FillHSpace"})
and then in your controller:
model.SelectedHabilitacoes = "123"
DropdownListFor has overloads to accomplish this
DropDownListFor([SelectedValue], [SelectList], optional HTML Attributes)
Normally you would go
DropDownListFor(model => model.ID, (SelectList)ViewsBag.MySelectList, optional HTML Attributes)
The selected value will be automatically bound to the dropdown list, and will automatically be posted the the model.ID value
if this doesn't help id ask you to post your model.

Resources