[英]Submit selected dropdownList value in ASP.NET MVC project
I'm listing a list of clients in a partial view我在局部视图中列出客户列表
@{
List<Clients> clientsList = ViewBag.ClientsList;
}
<script src="~/Scripts/jquery-3.1.1.min.js"></script>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Clients <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
@foreach (Clients c in clientsList)
{
<li>@Html.ActionLink(c.NomClient, "Index", "Home", new {c.ID}, null)</li>
}
</ul>
</li>
What I want is, when a user click on a client in the dropdown list, it send the client to ID to a method on a controller, without clicking on a submit button, with or form for example.我想要的是,当用户单击下拉列表中的客户端时,它会将客户端发送到 ID 到控制器上的方法,而无需单击提交按钮,例如带有 或 表单。 I tried with an ActionLink, but only by passing the id in the URL and I would like to not have the clientId in the URL.
我尝试使用 ActionLink,但只能通过在 URL 中传递 id 并且我不希望 URL 中包含 clientId。 How can I do that ?
我怎样才能做到这一点 ?
thx谢谢
Do as做为
HTML: HTML:
<ul class="dropdown-menu" role="menu">
@foreach (Clients c in clientsList)
{
<li>@c.NomClient</li>
}
</ul>
javascript: javascript:
// assuming you're using jQuery
$(".dropdown-menu").change( function (event) {
$.ajax({
url: "Home/Index/" + $(this).val(),
data: { id = $(this).val() /* add other additional parameters */ },
cache: false,
type: "POST",
dataType: "html",
success: function (data, textStatus, XMLHttpRequest) {
//do stuff
}
});
});
You can use Jquery for that.您可以为此使用 Jquery。
Create a class
on your li and an event on click on this class
.在你的 li 上创建一个
class
,并在点击这个class
创建一个事件。 Simply redirect your page with a window.location.href
and a @Url.Action()
.只需使用
window.location.href
和@Url.Action()
重定向您的页面。
JQuery查询
$(".dropdownclient").click(function(){
window.location.href="@Url.Action('Method','Controller')/"+$(this).data("id");
});
HTML HTML
<ul class="dropdown-menu" role="menu">
@foreach (Clients c in clientsList)
{
<li class='dropdownclient' data-id="@c.ClientID">@Html.ActionLink(c.NomClient, "Index", "Home", new {c.ID}, null)</li>
}
</ul>
Try this with jQuery.用 jQuery 试试这个。 Your ID will not be part of the URL.
您的 ID 不会成为 URL 的一部分。
<ul class="dropdown-menu" role="menu">
@foreach (Clients c in clientsList)
{
<li><button data-action="do-stuff" data-url="@Url.Action("Index", "Home")" data-id="@c.ID">@c.NomClient</button></li>
}
</ul>
$("[data-action='do-stuff']").on('click', function (event) {
var opt = $(this).data();
$.ajax({
url: opt.url,
data: { id = opt.id },
cache: false,
type: "POST"
success: function (data, textStatus, XMLHttpRequest) {
//do stuff
}
});
});
@Html.DropDownListFor(m => m.NomClient, new SelectList(ViewBag.ClientsList,"Key", "Value"), "Select", new { @class = "dropdown-menu" })
$(".dropdown-menu").change(function (event) {
$.ajax({
url: "Home/Index/",
data: { id = $(this).val() /* add other additional parameters */ },
type: "POST",
success: function (data) {
//do stuff
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.