2016-06-28 59 views
0
using (Html.BeginForm("Add", "Test", new { profil = Model.SelectedProfil }, FormMethod.Post)) 
{ 
    @Html.DropDownListFor(m => m.SelectedProfil, new SelectList(Model.Profils.Select(x => new { Value = x, Text = x }),"Value","Text")) 
    <button type="submit">Add</button> 
} 

異型材=字符串DropDownListFor剃刀列表<string>

SelectedProfil =字符串

SelectedProfil的名單總是空到控制器上後

+1

有你加入該路徑文件中的發佈操作的自定義路由?我說因爲你通過助手(Html.BeginForm(「Add」,「TestController」,new {profil = Model.SelectedProfil))使用的路由與通常默認生成的路由不同,因爲傳遞的參數總是這個名字叫做「id」,在這裏它叫做「profil」。只是一個想法,但是在我的默認路由配置中,我必須添加一個自定義路由,以便這樣的post操作可以工作。 –

+0

原來使用(Html.BeginForm(「Add」 ,「Test」,new {bd = Model.BD,appName = Model.SelectedAppName,profil = Model.SelectedProfil},FormMethod.Post))除profil參數以外的所有工作都是空的 – FrankSharp

+0

正常情況下,您必須添加自定義路由,因爲TestController應該命名爲Test – FrankSharp

回答

0

只需

using (Html.BeginForm("Add", "Test", FormMethod.Post)) 

@Html.DropDownList("profil", new SelectList(Model.Profils.Select(x => new { Value = x, Text = x }),"Value","Text"))) 

[HttpPost] 
public ActionResult Add(string profil) 
{ 
    .... 
} 
+1

根據你的問題使用'DropDownListFor()',刪除'new {profil = Model.SelectedProfil}'並且方法應該是'public ActionResult Add(string SelectedProfil) ' –