access the variable in my javascript from controller
In my CSHTML I have some JavaScript which conta开发者_开发技巧ins a string variable. How do I access this variable from my controller?
When you invoke the controller you could pass it as parameter. For example if you are invoking the controller action using AJAX:
$.post('/someaction', { someValue: someVariable }, function() {
...
});
[HttpPost]
public ActionResult someAction(string id)
{
return Content("got it");
}
in script
$(function(){
var someid='12';
$.post('/Controller/someAction',{id:someid},function(data){
//this call back is executed upon successfull POST
console.log(data); // got it"
});
});
You can use a hidden input field in your form valorized with the value of your string variable. When you post the form you can read the value as usual.
精彩评论