How to access values of the static dropdown in asp.net mvc - asp.net-mvc

#Html.DropDownList("FilterOptions", new List<SelectListItem>()
{ new SelectListItem { Text = "Begins with", Value = "1" },
new SelectListItem { Text = "Contains", Value = "2" } ,
new SelectListItem { Text = "Doesn't contain", Value = "3" } ,
new SelectListItem { Text = "Ends with", Value = "4" },
new SelectListItem { Text = "Doesn't equal", Value = "5" }
}, "Select filter options", new { #class = "large" })
The code given above is for a static dropdown for my search functionality.
How can I access the value of the dropdown in my Controller?

Related

Choose which option is selected on pageload of razor html.dropdownlistfor

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

ASP .NET MVC - DropDownList OnChange

I want to hide some data when I select a value in my dropdownList.
Example :
When I choose Gender = M, I don't want to see in my Title : Mr. Only Miss or Madame.
Here is my code :
#Html.DropDownListFor(m => m.Gender, new[] {
new SelectListItem() {Text = "M", Value = "M"},
new SelectListItem() {Text = "F", Value = "F"},
}, "---Choose Gender---", new { onchange = "Select();" })
#Html.DropDownListFor(m => m.Title, new[] {
new SelectListItem() {Text = "Mister", Value = "Mr."},
new SelectListItem() {Text = "Madame", Value = "Mme."},
new SelectListItem() {Text = "Miss", Value = "Miss."}
}, "---Choose Title---")
In Javascript Section :
function Select() {
// the code.
}
You can do this with the help of JQuery,
function changeGender()
{
if($('#Gender').val()=='M')
{
$('#Title').html('');
$('#Title').html('<option value="Mister">Mr.</option>');
}
else if($('#Gender').val()=='F')
{
$('#Title').html('');
$('#Title').html('<option value="Madame">Madam</option><option value="Miss">Miss.</option>');
}
else
{
$('#Title').html('');
}
return false;
}
Hope this helps.

#Html.DropDownListFor How to add option?

#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.

The selected item does not display when I use DropDownListFor

I am using the following to generate a drop down list:
#for (var index = 0; index < Model.AdminSummaries.Count(); index++)
{
<div class="rep_tr0">
<div class="rep_td0">
#Html.DropDownListFor(x => Model.AdminSummaries[index].Status,
AdminStatusReference.GetAdminStatusOptions(),
new { id = string.Format("Status_{0}",index ) })
</div>
</div>
}
Here's the HTML it generates:
<select id="Status_1" name="AdminSummaries[1].Status"><option value="1">Released</option>
<option value="2">Review</option>
<option value="3">New</option>
</select>
Here's the class that gives the status options.
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptions()
{
return new[]
{
new SelectListItem { Value = "1", Text = "Released" },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
}
}
Everything works good EXCEPT it doesn't select the items correctly. There's no option with 'selected' to match the data in the AdminSummaries.
How can I make it so the correct select list items are selected?
Just to clarify this. My problem is that if there is a data record with a value of 3 for the status then when I look at the screen I see a select list with the word "Release" showing.
What I need is for the select list to show text that corresponds with the data value.
Here is the more accurate answer
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptionsFor(AdminSummaries arg)
{
var options = new[]
{
new SelectListItem { Value = "1", Text = "Released" },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
options.First(o=> o.Value == arg).Selected = true;
return options;
}
}
Set the SelectListItem.Selected property to true:
public static class AdminStatusReference
{
public static IEnumerable<SelectListItem> GetAdminStatusOptions()
{
return new[]
{
new SelectListItem { Value = "1", Text = "Released", Selected = true },
new SelectListItem { Value = "2", Text = "Review" },
new SelectListItem { Value = "3", Text = "New" }
};
}
}
It seams from the source code that the DropDownListFor method (actually the ViewDataEvaluator.Eval method) doesn't support expressions containing indexers. Because your expression: AdminSummaries[index].Status contains an indexer that's why the framework doesn't use the selected value from your model class.
The only solution is to specify the selected item when setting the SelectListItem collection, you can do this by passing the currently selected value to your GetAdminStatusOptions method:
View:
#Html.DropDownListFor(x => Model.AdminSummaries[index].Status,
AdminStatusReference.GetAdminStatusOptions(Model.AdminSummaries[index].Status),
new { id = string.Format("Status_{0}",index ) })
A sample GetAdminStatusOptions implementation:
public static IEnumerable<SelectListItem> GetAdminStatusOptions(string selected = null)
{
var options = new[]
{
new SelectListItem {Value = "1", Text = "Released"},
new SelectListItem {Value = "2", Text = "Review"},
new SelectListItem {Value = "3", Text = "New"}
};
foreach (var option in options)
{
option.Selected = option.Value == selected;
}
return options;
}

Bind Html.DropDownList with static items

I have to bind an Html.DropDownList with just two items statically.
Text="Yes" Value="1"
Text="No" Value="0"
The important thing is that, I have to set the text and value fields.
How can I do this?
I used this is properly working
#Html.DropDownList("Status", new List<SelectListItem>
{
new SelectListItem{ Text="Active", Value = "1" },
new SelectListItem{ Text="Not-Active", Value = "0" }
})
It is a best practice not to create the SelectList in the view. You should create it in the controller and pass it using the ViewData.
Example:
var list = new SelectList(new []
{
new { ID = "1", Name = "name1" },
new { ID = "2", Name = "name2" },
new { ID = "3", Name = "name3" },
},
"ID", "Name", 1);
ViewData["list"]=list;
return View();
you pass to the constratctor: the IEnumerable objec,the value field the text field and the selected value.
in the View:
<%=Html.DropDownList("list",ViewData["list"] as SelectList) %>
Code below assumes you are using razor view engine if not you will need to convert it.
#{
var listItems = new List<ListItem>();
listItems.Add(new ListItem{Text="Yes", Value="1"});
listItems.Add(new ListItem{Text="No", Value="0"});
}
#Html.DropDownListFor(m=>m.SelectedValue, listItem);
You should consider creating the model in your code instead of the view. Also this would be a good candidate for an editor template.
if you want to be alittle explicity then try
#{
var domainsList = new SelectList(new []
{
new SelectListItem { Text = ".Com", Value = ".com", Selected = true },
new SelectListItem { Text = ".Shopping", Value = ".shopping"},
new SelectListItem { Text = ".Org", Value = ".org"},
new SelectListItem { Text = ".Net", Value = ".net"},
new SelectListItem { Text = ".AE", Value = ".ae"},
new SelectListItem { Text = ".Info", Value = ".info"},
}, "Value", "Text");
}
#Html.DropDownList("TopLevelDomains", domainsList)
This solved it for me:
<td>
#{ var RlistItems = new List<SelectListItem>();
RlistItems.Add(new SelectListItem { Text = "Select Room Type", Value = "0" });
RlistItems.Add(new SelectListItem { Text = "Meeting Room", Value = "1" });
RlistItems.Add(new SelectListItem { Text = "Office", Value = "2" });
RlistItems.Add(new SelectListItem { Text = "Cafeteria", Value = "3" });
}
#Html.DropDownListFor(model=>model.FirstOrDefault().RoomType
,RlistItems,RlistItems[item.RoomType.Value].Selected=true )
</td>
<select asp-for="CountryName" asp-items=#(new List<SelectListItem> { new SelectListItem {Text="India",Value="1" } ,new SelectListItem {Text="Japan",Value="2" }} ) class="form-control">
<option>SELECT COUNTRY -- </option>

Resources