我想創建一個頁面,在下拉框中顯示不同的時間(取決於選擇)。html.dropdownlistfor得到選定的值
我創建了頁面的下拉列表,但我不知道燙從中獲得選擇的值,並顯示在正確的時間在文本框中
這是我的模型:
namespace WebApplication2.Models {
public class Countries
{
public List<Country> CountriesList = new List<Country>();
public int SelectionID { get; set; }
public Countries()
{
Country CooseCountry = new Country { Name = "Choose country", ID = 1};
Country Israel = new Country { Name = "Israel - Tel-Aviv", ID = 2};
Country USA = new Country { Name = "USA - New -York", ID = 3};
Country Germany = new Country { Name = "Germany - Berlin", ID = 4};
CountriesList.Add(CooseCountry);
CountriesList.Add(Israel);
CountriesList.Add(USA);
CountriesList.Add(Germany);
}
} }
控制器:
public class TlvTimeController : Controller
{
public ActionResult Index()
{
ModelDateTime MyTime = new ModelDateTime();
Countries CountriesList = new Countries();
return View(CountriesList);
}
}
景觀:下面
@model WebApplication2.Models.Countries
@{
ViewBag.Title = "Index";
}
<h2>
@Html.DropDownListFor(m => m.SelectionID,
new SelectList(Model.CountriesList,
"ID", "Name"))
@Html.Label("The time")
</h2>
<td> <input type="button" value="Show the time" id="ShowTime" /></td>
,你需要處理按鈕的'.click()'事件並使用ajax將選定的值傳遞給一個控制器方法,該方法返回基於該值的時間(和'var selectedCountry = $('#SelectionID')。val );'返回所選選項的值) –
所以你是n點擊按鈕後,你的下拉列表中沒有選定的值?我對嗎? –
是的大衛,你的權利 – MrB