下拉列表 onChange 传递 URL MVC
Dropdownlist onChange pass URL MVC
我使用下拉列表并传递 url onchange 事件,如下所示
@onchange = "windows.location.href='Url.Action('Index', 'StudentManagement')?Id='this.options[this.selectedIndex].value+'&isDropdownValue=true'"
但是在加载视图时出现语法错误,因为 url 正在按如下方式转换
onchange="windows.location.href='Url.Action('Index', 'StudentManagement')?Id='this.options[this.selectedIndex].value+'&isDropdownValue=true'"
有人请建议我传递多个参数的正确方法。
尝试如下:
var action = Url.Action("Index", "StudentManagement", new { Id = this.options[this.selectedIndex].value,isDropdownValue=true });
@Html.DropDownListFor(model => model.YourModelName, Model.list, new{@class = "form-control",@onchange = "window.location.href='"+action+"'"});
您得到上述查询字符串是因为,您提到的单引号连同其他值已转换为等效的 Ascii 代码。
要避免这种情况:
在 Mvc 的剃刀视图中,您应该始终使用 @ 字符作为服务器端代码,在您的情况下 Url 是服务器端代码。
因此您的查询字符串将如下所示:
@onchange = "windows.location.href='@Url.Action('Index', 'StudentManagement')?Id='this.options[this.selectedIndex].value'&isDropdownValue=true'"
并且在id值之后你不需要+符号,它会自动连接。
希望代码一定能解决您的问题,请让我知道您的想法或反馈
谢谢
卡尔提克
我试过下面的代码并且工作正常
@Html.DropDownListFor(model => model.StudentId, Model.LstStudent, "-- Select --", new { id = "ddlStudent", @class = "form-control" })
$("#ddlStudent").change(function () {
var selectedStudentVal = $("#ddlStudentoption:selected").val();
window.location.replace("/StudentManagement/Index?Id=" + selectedStudentVal + "&isddlValue=true");
});
我使用下拉列表并传递 url onchange 事件,如下所示
@onchange = "windows.location.href='Url.Action('Index', 'StudentManagement')?Id='this.options[this.selectedIndex].value+'&isDropdownValue=true'"
但是在加载视图时出现语法错误,因为 url 正在按如下方式转换
onchange="windows.location.href='Url.Action('Index', 'StudentManagement')?Id='this.options[this.selectedIndex].value+'&isDropdownValue=true'"
有人请建议我传递多个参数的正确方法。
尝试如下:
var action = Url.Action("Index", "StudentManagement", new { Id = this.options[this.selectedIndex].value,isDropdownValue=true });
@Html.DropDownListFor(model => model.YourModelName, Model.list, new{@class = "form-control",@onchange = "window.location.href='"+action+"'"});
您得到上述查询字符串是因为,您提到的单引号连同其他值已转换为等效的 Ascii 代码。
要避免这种情况:
在 Mvc 的剃刀视图中,您应该始终使用 @ 字符作为服务器端代码,在您的情况下 Url 是服务器端代码。
因此您的查询字符串将如下所示:
@onchange = "windows.location.href='@Url.Action('Index', 'StudentManagement')?Id='this.options[this.selectedIndex].value'&isDropdownValue=true'"
并且在id值之后你不需要+符号,它会自动连接。
希望代码一定能解决您的问题,请让我知道您的想法或反馈
谢谢 卡尔提克
我试过下面的代码并且工作正常
@Html.DropDownListFor(model => model.StudentId, Model.LstStudent, "-- Select --", new { id = "ddlStudent", @class = "form-control" })
$("#ddlStudent").change(function () {
var selectedStudentVal = $("#ddlStudentoption:selected").val();
window.location.replace("/StudentManagement/Index?Id=" + selectedStudentVal + "&isddlValue=true");
});