[英]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.