好了,所以我是用一個文本框是這樣的:如何從DropDownList中獲取文本在.NET的MVC?
@Html.TextBoxFor(u=>u.Time)
現在我想用一個下拉列表替換文本框,我這樣做:
@Html.DropDownList("FromDD",new SelectList (new[] {"first","second","third"}))
我怎樣才能得到從下拉列表的文本,並將其保存在模型(Model.Time)?
好了,所以我是用一個文本框是這樣的:如何從DropDownList中獲取文本在.NET的MVC?
@Html.TextBoxFor(u=>u.Time)
現在我想用一個下拉列表替換文本框,我這樣做:
@Html.DropDownList("FromDD",new SelectList (new[] {"first","second","third"}))
我怎樣才能得到從下拉列表的文本,並將其保存在模型(Model.Time)?
鑑於你的榜樣,你正在尋找將發佈一個名爲字符串值FromDD。你作爲第一個參數使用字符串將數據粘合劑將您的值綁定到屬性的名稱。
如果你願意,你可以使用強類型的過載
@Html.DropDownListFor(p => p.Time, new SelectList (new[] {"first","second","third"}))
這回發的數據作爲一個名爲時間串,酷似你的文本框一樣。
,你可以把它作爲一個參數動作方法
[HttpPost]
public ActionResult ActionName(string FromDD){
}
表單要有action屬性設置爲ActionName:
<form action="ActionName">
...
@Html.DropDownList("FromDD",new SelectList (new[] {"first","second","third"}))
您可以使用
@ Html.DropDownListFor(u => u.Time,new SelectList(new [] {「first」,「second」,「third」}))
@Html.HiddenFor(m=>m.Time new{@id='hdnTime'})
在客戶端從下拉菜單中您提交
$('#hdnTime').val($('#fromDD option:selected').text())
對不起,那是不是故意的,我只是發現你是正確的 –
燁之前設置這一點,這就是我想要的,謝謝! – user2962142