0

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" />
}

1 Answer 1

2
// 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
   })
});
Sign up to request clarification or add additional context in comments.

6 Comments

i want to send it to server when my asp.net button is clicked, & if user selects the other name then can listcontent be change?
@JeloMelo during each click on list the listcontent will change
thanx, one more question, can i achieve the same thing when user on swipeleft, on swiperight event of jquery mobile?
@JeloMelo Yup possible. you are welcome, best way to thank any one who's answer you choose is accepting that answer. Thanks
then what do i need to write? $('ul li').on('swipeleft', function() { listcontent = $(this).text(); ??? });
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.