[英]ASP.NET MVC2 Dropdownlist and URL.action
我有一个Dropdownlist,它需要在其OnChange事件上使用选定的Dropdownlist值的参数值调用控制器上的操作。
这就是我现在所拥有的。
<%= Html.DropDownList("series",
new SelectList((IEnumerable)ViewData["series"], "ProductId", "SeriesName"),
new { onchange = "??" })%>
我可以将列表包含在表单元素中
<form id="SeriesForm" action="<%: Url.Action("S", "ProductSearch", new {SeriesId = ?? }) %>" method="post">
但是,如何设置应该作为下拉列表的选定值的SeriesId参数? 我尝试将jQuery方法附加到OnChange事件,但无法更改Url.Action方法。
谢谢,
一种非常简单的方法是将您要调用的网址用作下拉菜单中选项的值。 还有其他方法可以做到,但这是最基本的方法:
<select id="myList" ...>
<% foreach (var id in ids) { %>
<option value="<%= Url.Action("Action", "Controller", new { id = id }) %>">id</option>
<% } %>
</select>
然后从jQuery中获取这样的网址:
$('#myList').change(function(){
$.get($(this).val(), doOnSuccess);
});
其他执行此操作的方法:
$(this).val()
请求$(this).val()
您还可以请求存储在某个位置的某些url并将$(this).val()
作为数据部分传递: $.get($('#storeMyUrl').val(), $(this).val(), doOnSuccess)
编辑:这是第一种选择的更详细说明(为方便起见,在Razor中完成,为清晰起见,使用了显式html(希望如此)):
<form id="theForm" action="@Url.Action("A", "C")" method="POST">
@Html.DropDownList("paramName", theSelectListAsInTheQuestion, new { id = "theDropDown" })
</form>
...
public class CController : Controller {
[HttpPost]
public ActionResult A (int paramName) {
...
}
}
...
// and in jQuery
$('#theDropDown').change(function(event){
var f = $('#theForm');
$.post(f.attr('action'), f.serialize(), onSuccess);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.