0

In my CSHTML I have some JavaScript which contains a string variable. How do I access this variable from my controller?

3 Answers 3

4

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

Comments

1
[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"

});
});

Comments

1

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.

Comments

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.