2011-10-19 24 views
0

我有這段代碼,我怎麼能得到@htmlDropDownList的選定值來把這個值放在我的@ Url.Action中而不是,我已經使用javascriptØjQuery的閱讀,但我無法弄清楚如何可以在視圖中直接做的,這是不工作我我怎麼能在@Html.DropDownList中選擇一個值在MVC3

var idempresa = $('IdEmpresa').val() 
<a href="@Url.Action("List","Script")?idempresa=**idempresa**" 

代碼:

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Script</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.IdEmpresa, "Empresa") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("IdEmpresa", String.Empty) 
      @Html.ValidationMessageFor(model => model.IdEmpresa) 
     </div> 
    </fieldset> 


} 


    href="@Url.Action("List","Script")?idempresa=**1**" 

PD。感謝你的幫助

編輯:

@using System.Web.Mvc.Html 
@model SistemaDispositivos.Models.Script 

@{ 
    ViewBag.Title = "Script"; 
} 

<h2>Script</h2> 
<script src="../../Scripts/jquery-1.6.2.min.js" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Script</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.IdEmpresa, "Empresa") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("IdEmpresa", String.Empty) 
      @Html.ValidationMessageFor(model => model.IdEmpresa) 
     </div> 
    </fieldset> 
} 

<script type="text/javascript" language="javascript"> 
    $(document).ready(function() { 
     var url1 = '@Url.Action("List","Script")?idempresa='; 
     $('IdEmpresa').change(function() { 
      $urlId = $('urlid'); 
      $urlId.attr('href', url + $(this).val()); 
     }); 
    }); 

</script> 


<a id="urlid" href="@Url.Action("List","Script")?idempresa=url1"> </a> 

什麼IAM缺失?

+0

是什麼你的錯誤?如果沒有這些,不能告訴你什麼是缺失的。 – mccow002

回答

2

以及它的工作,現在這就是我想要的幫助感謝:在div利斯塔腳本

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>Script</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.IdEmpresa, "Empresa") 
     </div> 
     <div class="editor-field"> 
      @Html.DropDownList("IdEmpresa", string.Empty ) 
      @Html.ValidationMessageFor(model => model.IdEmpresa) 
     </div> 
    </fieldset> 
} 

<script type="text/javascript" language="javascript"> 
    function valid() { 
     var listaScriptUrl = '@Url.Action("List", "Script", new { idempresa = 0})'; 
     var input = $('#IdEmpresa').val(); 
     $('#Lista-Script').load(listaScriptUrl.replace('0', input)); 
    } 
</script> 

<input type="button" value="Click Here" onclick="valid()"> 

<div id="Lista-Script"> 

</div> 

所以現在當我按一下按鈕,我得到我的看法,我想

0

該視圖將呈現HTML,因此您無法獲取下拉列表的選定值並將其放回已在服務器端執行的視圖的變量。你可以做的就是在標籤上添加一個id並更新下拉菜單的變化事件的href。

$(document).ready(function(){ 
    $('IdEmpresa').change(function(){ 
     var val = $(this).val(); 
     var newurl = $("urlid").attr("href") + val; 
     $("urlid").attr("href",newurl); 
    }); 
}); 

<a id="urlid" href="@Url.Action("List","Script")?idempresa="> </a> 
+0

這似乎沒有考慮到用戶多次更改下拉菜單。這看起來好像只是將值附加到第二次更改下拉列表的位置,url將無法使用。 – mccow002

+0

是的。在這種情況下,不完整的URL應該存儲在其他地方,並且每次更改時都會填充href的新值。 – Birey

+0

它只需要改變一次下拉單擊鏈接,以便鏈接將轉到一個視圖 我得到和錯誤去了我這樣做,我怎麼能在這個 @ Html.DropDownList(「IdEmpresa」, String.Empty,new {onchange =「showTextBox(this)」}) –

0

我會盡可能接近Bipins的貼子,只有一個例外。

<fieldset> 
    <legend>Script</legend> 

    <div class="editor-label"> 
     @Html.LabelFor(model => model.IdEmpresa, "Empresa") 
    </div> 
    <div class="editor-field"> 
     @Html.DropDownList("IdEmpresa", *This needs to be a list of ListItems*, new { id = "idEmpresa" }) 
     @Html.ValidationMessageFor(model => model.IdEmpresa) 
    </div> 
</fieldset> 

$(document).ready(function() 
{ 
    var url = '@Url.Action("List","Script")?idempresa='; 
    $('#idEmpresa').change(function() 
    { 
     $urlId = $('urlid'); 
     $urlId.attr('href', $urlId.attr('href') + $('this').val()); 
    } 
}); 

通過將原始URL緩存到js變量中,您始終擁有一個乾淨的石板以附加到。

+0

你剛剛寫的看起來很好。 – mccow002

+0

我不爲什麼不工作,運行應用程序在下拉列表中選擇一個項目,它不會執行任何操作:S iam缺少一個使用或某事我只是更新我的代碼上面的問題 –

+0

所以你說的href屬性不會更改? – mccow002

相關問題