2012-10-02 70 views
4

我正在爲註冊事件放在一起的questionairre。ASP.Net MVC查看與RadioButtonList與其他自由格式選擇

之一eventr註冊問題是:

What is your main job function? 
    Director/MD/Owner 
    Manager 
    Team Leader 
    Other: Please state 

我想這顯示爲一個下拉框,或者在我看來,一個單選按鈕列表,但也有一個文本框的下方或旁邊「其他」單選按鈕,這樣就可以輸入一個自由形式的「其他」作業功能。在我看來,我將如何約束這一點?

我懷疑我的模型將包含:

[Display(Name = "What is your main job function?")] 
    public string JobFunction { get; set; } 

...其中JobFunction得到的由上述所選擇的項目填充。但是,在我的控制器中,如果它是「其他」並將其替換爲視圖上的文本框,我是否會覆蓋所選項目?

我敢肯定,這之前已經做過很多次 - 所以感謝您的幫助,

馬克

許多

回答

2

一種方法是這樣的:

查看:

<div id="sample-dropdown"> 
    <select name="JobFunction " id="JobFunction" class="dropdown"> 
     <option value="Director/MD/Owner">Director/MD/Owner</option> 
     <option value="Manager">Manager</option> 
     <option value="Team Leader" selected="selected">Team Leader</option> 
     <option value="Other">Other: Please state</option> 
    </select> 
    <input name="JobFunctionOther" id="JobFunctionOther" /> 
</div> 

<script type="text/javascript"> 
    $('input[name="JobFunctionOther"]').hide(); 
     $(function() { 
       $("#JobFunction").change(
        function() { 
         var val = $(this).val(); 
         if (val =='Other') { 
          $('input[name="JobFunctionOther"]').show(); 

         } else { 
          $('input[name="JobFunctionOther"]').hide(); 
         } 

        }).change(); 
      }); 
</script> 

控制器:

public ActionResult DropDown(string jobFunction, string jobFunctionOther) 
{ 
    if (!string.IsNullOrEmpty(jobFunctionOther)) jobFunction = jobFunctionOther; 
    //do what you do 
    return View(jobFunction); 
}