0

I have a jQuery script which let's me search/filter through a list of people and select them. Using this, it allows me to set a div based on that person's traits.

However, I need to be able to use this variable later on (after I post the form.)

Is there a way to set the model variable for the personID in the jQuery? i.e.:

<%: Model.PersonId == ui.item.id; %>

Or is there another way to do this?

Also - I am posting the form using a submit button, so if there was a way I could set Html.Hidden(), then that would work also.

asked Aug 31, 2011 at 14:07
1
  • Yeah, that makes sense. How would you set a hidden like this using jQuery? Commented Aug 31, 2011 at 14:13

2 Answers 2

1

You can create a hidden field using javascript, set its value with whatever value you need and then submit it along with the form. You can then access this hidden field value in your controller.

$("form").append("<input type='hidden' name='hiddenPersonId' value='"+ui.item.id+"' />");

Alternatively

$("form").append($("<input type='hidden' name='hiddenPersonId'/>").val(ui.item.id));

On the server side you can get this value using

Request.Form["hiddenPersonId"];
Neil N
25.3k17 gold badges87 silver badges148 bronze badges
answered Aug 31, 2011 at 14:13

3 Comments

Awesome! one question, why do you have the '"+ui.item.id+"' enclosed that way? with single quotes and inside "+item+"?
Because ui.item.id contains the value which you want to post to the server. We have to set the hidden fields value and that is how we concatenate the string.
I have added the alternative way also take a look.
0

The model is on the server and jquery is on the client, so the code you posted won't work. There are a number of ways to do this. Your javascript could set a hidden form field, or post an ajax request to the server which would fetch an updated model.

 <html>
 <head>
 <script>
 $(document).ready(function() {
 $("#submit-button").click(function(){
 $("#hidden-input").val(new Date()); //set the value to the current date
 });
 });
 </script>
 </head>
 <body>
 <form>
 <input type="hidden" id="hidden-input" name="hidden-input" />
 <input type="submit" id="submit-button" value="submit" />
 </form>
 <body>
</html>
answered Aug 31, 2011 at 14:10

2 Comments

Could you please provide an example of setting a hidden form field in java?
that's untested, but pretty close.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.