2010-02-06 19 views
22

我在MVC視圖中有一個下拉列表。在選擇更改下拉列表時,我想調用控制器中的特定操作方法。在MVC中的下拉列表選擇上調用特定操作

我有看法這樣做是:

<%=Html.DropDownList("ddl", ViewData["AvailableList"] as SelectList, 
    new { onchange = "this.form.action='MyMethod';this.form.submit();" })%> 

一切都得到編譯。但是,當我改變下拉選擇運行時異常被拋出,因爲

「Microsift JScript運行時錯誤:對象不支持屬性或方法」

我怎樣才能重定向到具體的行動上列表選擇更改事件?
如何將參數傳遞給此操作方法?

回答

13

你的方法應該工作。您遇到的問題是您在onchange事件中使用this無效。試着像這樣的東西取代您的this.form引用:

<%= Html.DropDownList(
     "ddl", ViewData["AvailableList"] as SelectList, 
     new { onchange = @" 
      var form = document.forms[0]; 
      form.action='MyMethod'; 
      form.submit();" 
     }) %> 
+0

美麗!!!!!! – Seva 2015-03-31 13:58:53

18

你確實需要提交表單嗎?您可以重定向:

其中redirect是一個自定義的功能:

function redirect(dropDownValue) { 
    window.location.href = '/myController/myAction/' + dropDownValue; 
} 
+1

我試過,但如果我傳遞參數作爲查詢字符串它才能正常運行,例如:window.location.href ='/myController/myAction?param ='+ dropDownValue; – fjxx 2011-04-15 02:36:30

+1

@fjxx,你應該可以通過重新配置你的[URL路由]來解決這個問題(http://goo.gl/fKcxX)。 – Shimmy 2012-11-25 09:55:22

+0

@Darin Dimitrov,有沒有辦法做到這一點作爲一個單一的,而不是在js中定義函數? – Shimmy 2012-11-25 09:56:00

6

這是一個jQuery的事情。給它一個班,然後連線。

Html.DropDownList("ddl", AvailableList, new { @class = "_select_change" }) 

一個粗略的腳本可能是這樣的:

$('._select_change').change(function() { $.post(ctrlUrl); }) 
2

都喜歡:d

我提出了一個混合 - 我想了很多jQuery的。

$('ddl').change(
function() { 
    // This contains your selected option val 
    $(this).val(); 

// so you can do domething like... 
    $.post(
    $(this).val(), 
    { Param1: xxxx, 
     Param2: xxxx, 
     Param3: xxxx }, 
    function(data) { 
    // handle your call here. 'data' contains the response 
    }); 
} 
2

從控制器動作,你要撥打電話以同樣的方式:

<%= Html.DropDownList(ddl, ViewData["items"] as SelectList, new { onchange = string.format("doSomething({0}); return false;", action) }) %> 

一旦你這樣做,把一個Javascript功能,你的頁面調用該方法上。但是,如何調用它將取決於它是否是AJAX調用。也就是說,你是否想要一個往返的頁面。對於非AJAX調用:

function doSomething(action) { 
    window.location.href = action; 
} 

如果它是一個AJAX調用:

function doSomething(action) { 
    $.load(action); 
} 

傳遞參數給動作,你只需要確保所有的數據元素,你想傳遞包含在<form>標記中。例如,假設您想在您的下拉列表中包含名字和姓氏。在視圖中,你會做這樣的事情:

<%= using (Html.BeginForm()) 
    { %> 
    <table> 
     <tr> 
      <td>First Name:</td> 
      <td><%= Html.TextBox("FirstName") %></td> 
     </tr> 
     <tr> 
      <td>Last Name:</td> 
      <td><%= Html.TextBox("LastName") %></td> 
     </tr> 
     <tr> 
      <td>Assign to:</td> 
      <td><%= Html.DropDownList(ddl, ViewData["items"] as SelectList, 
       new { onchange = string.format("doSomething({0}); return false;", ViewData["action"]) }) %></td> 
     <tr> 
    </table> 
<% } %> 

在JavaScript函數:

function doSomething(action) { 
    var firstName = $('#FirstName').val(); 
    var lastName = $('#LastName').val(); 
    $.load(action, { first: firstName, last: lastName }); 
} 
1

@Shimmy是有辦法做到這一點作爲一個班輪,但沒有界定 在JS的功能?

是的,你可以在這裏是該代碼:

@Html.DropDownListFor(m => m.Name, new SelectList((System.Collections.IEnumerable)ViewBag.DropDownName, "Value", "Text"), new { @class = "form-control", onchange = "document.location.href = '/Home/Employee?c=' + this.options[this.selectedIndex].value;" })