簡體   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