2017-05-03 44 views
0

我查看代碼MVC 4:我如何將選定的值DropDownList作爲QueryString參數傳遞?

<tr bgcolor="#FFFFFF"> 
    <td height="36" align="right" bgcolor="#F7F7F7"><strong>System</strong>&nbsp;&nbsp;</td> 
    <td height="36"> 
     &nbsp;&nbsp;&nbsp; 
      @Html.DropDownListFor(model => model.CSNumber, new SelectList(ViewBag.System, "Value", "Text", Model.CSNumber), "--- All ---", new { @class = "box", id = "ddlSystem" }) 
    </td> 
</tr> 
<tr bgcolor="#FFFFFF"> 
    <td height="36" align="right" bgcolor="#F7F7F7"></td> 
    <td height="36"> 
     &nbsp;&nbsp;&nbsp;System = @Html.Value(**"ddlSystem"**) 
     <a class="" href="~/jobs/Create?system=<<Currently Selected Value of **"ddlSystem"** up above>>" id="createLink" style="text-decoration: underline">NEW REQUEST</a> 
    </td> 
</tr> 

我如何通過當前選定的ddlSystem下拉值,當用戶點擊,即可創建新的請求LINK?

回答

0

我會做與jQuery,像這樣:

$(function() { 
    $("#createLink").click(function() { 
     var selectedValue = $('#ddlSystem option:selected').val(); 

     window.location = "@Url.Action("Create", "Jobs")?system=" + selectedValue; 
    }); 
}); 
0

你可以寫JQuery的如下所示,這實際上是設置你的鏈接的「href」屬性。通過這種方式,當用戶更改下拉值時,「href」將被更新。

<script> 
$(document).ready(function() { 
    function setBtnUrl() { 
     $('#createLink').attr('href', '/jobs/Create?system=' + $("#ddlSystem").val());// here it update the URL based on selected value 
    } 
    $("#ddlSystem").change(function() { 
     setBtnUrl(); 
    }); 
    setBtnUrl();// set the selected value on page load. 

}); 
</script> 
相關問題