簡體   English   中英

c#mvc4 Html.DropDownList用於調用控制器

[英]c# mvc4 Html.DropDownListFor calling the controller

我瀏覽了幾個帖子和幾個代碼,但似乎都沒有用。

我需要基於同一頁面上的DropDown選定鍵(部分視圖)重新加載WebGrid。 我現在正在構建dropdown元素,並在視圖中使用以下代碼:

@Html.DropDownListFor(model => model.Users, Model.Users, 
                                         new { autopostback = "true" })

以及以下javascript代碼:

<script type="text/javascript">
    $(document).ready(function () {
        $('select:[autopostback=true],input[type=checkbox]:[autopostback=true],input[type=radio]:[autopostback=true]')
      .live('change',function () {
            $(this).closest('form').submit();
        });
    });
</script>

瀏覽器上的Javascript控制台說:

Uncaught Error:Syntax error, unrecognized expression:select:[autopostback=true],
         input[type=checkbox]:[autopostback=true],
         input[type=radio]:[autopostback=true] 

Controller上沒有任何呼叫。 我究竟做錯了什么?

謝謝。

[編輯]

由於現在工作正常,因此可以進行以下工作:

<script type="text/javascript">
    $(function () {
        $("#UserList").change(function (e) {
            var _this = $(this);

            $.post("@Url.Action("List","MainController", Model)", _this.closest("form").serialize(),
                                                             function (response) {
                                                                 // do something with response.
                                                             });
  });

和視圖:

                <td class="tdatadata">@Html.DropDownList("UserList", Model.Users, new { autopostback = "true" })</td>

和ViewModel:

public class ModelViewModel
{
    public int idSelected { get; set; }

    [Display(Name = "Usuários Cadastrados")]
    public IEnumerable<SelectListItem> Users { get; set; }
}

但是我仍然有一個問題:如何將所選字段傳遞給控制器​​動作? 我嘗試使用DropDownListFor,但在那種情況下,我松開了對象名稱,而Jscript無法使用它們。

嘗試這個:

$(document).ready(function () {
    $('select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]').live('change',function () {
        $(this).closest('form').submit();
    });
});

從選擇器中刪除列“:”,這是一個元字符,使選擇器無效,也不需要它們。

另外,嘗試使用id或獲取表單的所有輸入字段,請使用jquery:input選擇器 $('form :input')選擇表單的所有輸入字段。

$('form').find(':input').live('change',function () {
     $(this).closest('form').submit(); 
});

另請注意,不建議使用live,如果您使用的jQuery版本> = 1.7,請使用on()代替live

嘗試刪除:並通過.on更改.live

$(document).ready(function () {
    $('select[autopostback=true],input[type=checkbox][autopostback=true],input[type=radio][autopostback=true]').on('change',function () {
        $(this).closest('form').submit();
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM