Reputation: 1240
I don't know if this is possible -- Googling has turned up nothing. But I would like to have a DropDownList display the value for its selected index on selection change.
Any advice is appreciated.
Regards.
Upvotes: 0
Views: 941
Reputation: 3636
Here is the dropdown:
@Html.DropDownListFor(model => model.CountyID, new SelectList(Model.allCounties, "CountyID", "CountyName"), "Select.....", new { @class = "form-control", @id = "CountyID", @onchange = "DisplayCountyInfo();" })
<div id="CountyView">
@Html.Partial("_CountyView", Model._countyModel)
</div>
<script>
function DisplayCountyInfo() {
$("#CountyView").load('@(Url.Action("_CountyView", "Controller", null, Request.Url.Scheme))?id=' + $("#CountyID").val());
};
</script>
Create a partial view
@model Application.Web.ViewModels.CountyViewModel
<h4><b>@Model.CountyName</b></h4>
<a href="@Model.URL" target="_blank">@Model.URL</a>
Controller
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult _CountyView(int id)
{
var county = _countyService.Get(id);
CountyViewModel viewModel = new CountyViewModel()
{
CountyName = county.CountyName,
URL = county.URL
};
return PartialView("_CountyView", viewModel);
}
Upvotes: 0
Reputation: 9166
I think you are mixing up the vocabulary a bit. A regular drop down contains "items".
In plain html, a drop down is rendered as a <select ..>
element with <option ..>
elements representing the items.
In Asp.Net, the corresponding control is called DropDownList
and it contains ListItem
child elements. ListItems
has properties Text
and Value
, where the Text property is what is shown in the UI and the Value property is what is posted back to the server.
For the parent DropDownList
, SelectedValue
is the Value
attribute from the selected ListItem
. SelectedIndex on the othe hand is the zero based numerical index of the selected ListItem
among all the ListItems in the dropdown.
Vocabulary lesson out of the way, you seem to want to have a drop down where instead of showing text, an image would be shown in the UI and the user could then select the correct image. The value property of the dropdown would contain some sort of ID for the selected image (maybe an image file name, or a key from a database table or something like that).
To get you on you way with showing images in the dropdown, please have a look at the accepted answer to this question:
Upvotes: 1
Reputation: 4546
Value cannot be retreived, unless if you havent proved it when setting datasouce. You must proved DisplayMember (the items displayed) and ValueMember property to retreive value of selected item. If you didnt do so, you cannot get Vlaue (or selectedValue) of selected item.
Upvotes: 0