繁体   English   中英

有没有办法在asp.net mvc 3中使用JavaScript,Jquery来监听事件

[英]is there a way to listen to events using JavaScript, Jquery in asp.net mvc 3

我有一个从数据库中获取的名称列表,我想当用户选择任何一个名称时,其ID将保存在变量中然后提交我在服务器上收到此ID的表单。 我想知道它有可能吗? 我对这项技术很陌生。

这是我的代码

调节器

  public ActionResult Index()
        {
            MyUsers user = new MyUsers();
            List<MyUsers> result = user.GetAllUser();
            return View(result);
        }

视图

<h2>Products</h2>

@using (Html.BeginForm())
{
<ul>
@foreach (var item in Model)
{
    <li>@item.Name</li>
}
</ul>
    <input type="submit" value="Click Me" />
}
// To save the list content/id

var listcontent = '';
$('ul li').on('click', function() {
   listcontent = $(this).text(); // if you want id of list then : listcontent = this.id;
});

// Send the list data to server

$('input[type=submit]').on('click', function() {
   // assumed that you're sending GET reqeust
   $.get('url_to_script?val=' + listcontent, function(res) {
     // res contains data returned from server
   })
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM