简体   繁体   English

值未传递到控制器方法

[英]Value not passed to controller method

I have the following view Board.cshtml , where I want to render a partial view MessagesBoard.cshtml based on an event of DropDownListFor . 我有以下视图Board.cshtml ,在这里我想基于DropDownListFor事件渲染部分视图MessagesBoard.cshtml

Boards.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>

The Controller method: UpdateBoardMessages does not have a value for the val sent from the jQuery script. Controller方法:UpdateBoardMessages没有从jQuery脚本发送的val的值。

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

What am I doing wrong? 我究竟做错了什么?

You actually need to use Url.Action to generate the correct url instead of using magic strings and then you can add the query string parameter by string conctenation in javascript something like: 实际上,您实际上需要使用Url.Action生成正确的url而不是使用魔术字符串,然后可以通过javascript中的字符串约束添加查询字符串参数,例如:

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

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM