简体   繁体   中英

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. 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
   })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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