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.
-
Yeah, that makes sense. How would you set a hidden like this using jQuery?Cody– Cody2011年08月31日 14:13:08 +00:00Commented Aug 31, 2011 at 14:13
2 Answers 2
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"];
3 Comments
'"+ui.item.id+"'
enclosed that way? with single quotes and inside "+item+"
?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.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>
Explore related questions
See similar questions with these tags.