2015-06-05 50 views
0

是否有可能將@Html.DropDownListFor當前值傳遞給動作鏈接?我想通過Create操作將模板值傳遞給Sample控制器。下面的代碼不起作用,因爲@Model.SurveyTemplate沒有返回任何值。是否需要JavaScript來檢索?請指導我。將html.dropdownlistfor當前值傳遞給Actionlink?

下拉列表:

@Html.LabelFor(model => model.Survey_Template, "Survey Template") 
       @Html.DropDownListFor(model => model.Survey_Template, new SelectList(
        new List<Object>{ 
         new { value = "Style1" , text = "Style1" }, 
         new { value = "Style2" , text = "Style2" }, 
         new { value = "Style3" , text = "Style3"} 
        }, 
        "value", 
        "text", 
      0)) 

的ActionLink:

@Html.ActionLink("_", "Create", "Sample", 
      new { template = @Model.Survey_Template }, 
      new { @id = "popup-link", @class = "classname2" }) 

回答

1

您有JavaScript來做到這一點,剃刀語法僅在服務器端認可。這意味着@Model.Survey_Template將在用戶請求頁面時呈現並具有默認值。

如果您選擇使用JavaScript,請記住您需要保存基礎網址並根據所選內容爲其添加參數。

此步驟將幫助你實現你想要的結果:

  1. 你的下拉附上change事件。
  2. 獲取選中value,構建新的URL
  3. 更換動作的網址(href)(<a .. />

注:,因爲我希望你能超越我沒有寫代碼你自己。


我想嘗試才達到它是一個formGET什麼。

@using(Html.BeginForm("Create", "Sample", FormMethod.GET)){ 
    @Html.LabelFor(model => model.Survey_Template, "Survey Template") 
    @Html.DropDownListFor(model => model.Survey_Template, new SelectList(
     new List<Object>{ 
      new { value = "Style1" , text = "Style1" }, 
      new { value = "Style2" , text = "Style2" }, 
      new { value = "Style3" , text = "Style3"} 
     }, 
     "value", 
     "text", 
    0)) 

    <input type="submit" value="_" class="classname2" id="popup-link" /> 
}