簡體   English   中英

值未傳遞到控制器方法

[英]Value not passed to controller method

我有以下視圖Board.cshtml ,在這里我想基於DropDownListFor事件渲染部分視圖MessagesBoard.cshtml

Boards.cshtml

<script>
    $(function () {
        debugger
        $("#selected_listBulletinBoardsMessages").change(function (e) {
            alert($(this).val());
            var val = $(this).val();
            $("#updateBulletMessagesView").load("UpdateBoardMessages/", { listBulletinBoardsMessages: val });
        });
    });
</script>
<div>
    @Html.DropDownListFor(model => model.listBulletinBoardsMessages, Model.listBulletinBoardsMessages.Select(b => new SelectListItem() { Value = b.Department, Text = b.Department }), "All Departments",
           new { @class = "form-control",
                 id = "selected_listBulletinBoardsMessages"
       })
</div>

<div id="updateBulletMessagesView">
    @Html.Partial("MessagesBoard")
</div>

Controller方法:UpdateBoardMessages沒有從jQuery腳本發送的val的值。

public ActionResult UpdateBoardMessages(string val)
        {
            val = val; //NULL??
            ..
            return PartialView("MessagesBoard.cshtml");
        }

我究竟做錯了什么?

實際上,您實際上需要使用Url.Action生成正確的url而不是使用魔術字符串,然后可以通過javascript中的字符串約束添加查詢字符串參數,例如:

$("#updateBulletMessagesView").load('@Url.Action("UpdateBoardMessages","ControllerName")'+'?val='+val;

希望這可以幫助。

暫無
暫無

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

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