asp.net html的onchange事件,下拉列表

fhity93d  于 2022-12-15  发布在  .NET
关注(0)|答案(7)|浏览(149)

我正在尝试为下拉列表的onchange事件触发一个action方法,如果不使用jquery onchange,我怎么能做到呢?

@Html.DropDownList("Sortby", 
                   new SelectListItem[] 
                   { 
                       new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, 
                       new SelectListItem() { Text = "Oldest to Newest", Value = "1" }})

谢谢

3ks5zfa0

3ks5zfa01#

如果你不想使用jquery,那么你可以使用javascript:-

@Html.DropDownList("Sortby", new SelectListItem[] 
{ 
     new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, 
     new SelectListItem() { Text = "Oldest to Newest", Value = "1" }},
     new { @onchange="callChangefunc(this.value)" 
});

<script>
    function callChangefunc(val){
        window.location.href = "/Controller/ActionMethod?value=" + val;
    }
</script>
k3bvogb1

k3bvogb12#

你能做到的

@Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() 
  { 

       Text = "Newest to Oldest", Value = "0" }, new SelectListItem() { Text = "Oldest to Newest", Value = "1" } , new
       {
           onchange = @"form.submit();"
       }
})
8yparm6h

8yparm6h3#

如果要将值传递给操作方法,可以尝试这样做。

@Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() { Text = "Newest to Oldest", Value = "0" }, new SelectListItem() { Text = "Oldest to Newest", Value = "1" }},new { onchange = "document.location.href = '/ControllerName/ActionName?id=' + this.options[this.selectedIndex].value;" })

如果没有参数传递,则删除查询字符串。

eivgtgni

eivgtgni4#

试试这个:

@Html.DropDownList("Sortby", new SelectListItem[] { new SelectListItem() 
{ Text = "Newest to Oldest", Value = "0" }, new SelectListItem() 
{ Text = "Oldest to Newest", Value = "1" }},
new { onchange = "document.location.href = '/ControllerName/ActionName?id=' + this.options[this.selectedIndex].value;" })
4ngedf3f

4ngedf3f5#

首先你需要给予你的下拉onchange事件;

@Html.DropDownList("ddl_reportId", new SelectList(ViewBag.ddl_reportName, "ddl_reportId", "ddl_reportName"), "Raporu seçin", new { @class = "form-control", @onchange = "getVal()" })

然后在脚本部分你必须这样调用它。

function getVal() {
    var selectedVal = document.getElementById("ddl_reportId").value;
    console.log(selectedVal)};
rqenqsqc

rqenqsqc6#

如果您有列表视图,则可以执行以下操作:
1.定义选择列表:

@{
   var Acciones = new SelectList(new[]
   {
  new SelectListItem { Text = "Modificar", Value = 
   Url.Action("Edit", "Countries")},
  new SelectListItem { Text = "Detallar", Value = 
  Url.Action("Details", "Countries") },
  new SelectListItem { Text = "Eliminar", Value = 
  Url.Action("Delete", "Countries") },
 }, "Value", "Text");
}

1.使用定义的SelectList,为每条记录创建一个不同的id(记住每个元素的id在视图中必须是唯一的),最后调用onchange事件的javascript函数(在示例url和record key中包含参数):

@Html.DropDownList("ddAcciones", Acciones, "Acciones", new { id = 
item.CountryID, @onchange = "RealizarAccion(this.value ,id)" })
  1. onchange函数可以是:
@section Scripts {
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

<script type="text/javascript">

function RealizarAccion(accion, country)
{

    var url = accion + '/' + country;
    if (url != null && url != '') {
        window.location.href = url ;
    }
}
</script>

@Scripts.Render("~/bundles/jqueryval")
}
jtw3ybtb

jtw3ybtb7#

如果您想在更改事件时调用Javascript函数,请尝试以下操作:@Html.DropDownList(“项目ID”,模型.项目,“选择项目”,新建字典〈字符串,对象〉{ {“类”,“选择2”},{“onchange”,“onProjectSelect()”} })

function onProjectSelect() {
    //write your business logic..
}

字符串

相关问题