简体   繁体   English

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

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

I have a list of names which is fetched from database, I want when user select any one of the name then its id will be saved in a variable & then on submit the form I receive this id on server. 我有一个从数据库中获取的名称列表,我想当用户选择任何一个名称时,其ID将保存在变量中然后提交我在服务器上收到此ID的表单。 I am wondering is it possible? 我想知道它有可能吗? I am pretty new to this technology. 我对这项技术很陌生。

Here's my code 这是我的代码

controller 调节器

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

View 视图

<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