I'm learning MVC 5 using C# and javascript. I've spent 2 hours searching solutions for converting javascript string to C# string and vice versa. But I don't know how to achieve this.
-
4Your question makes no sense. JavaScript code executes within the client browser and C# code executes on the IIS server. There is no shared memory. Are you asking how to pass a string argument between the two?Special Sauce– Special Sauce2015-06-24 17:43:46 +00:00Commented Jun 24, 2015 at 17:43
-
3Please supply some code to help explain what you are trying to achieve. Taken out of context, it's hard to understand what you are ultimately looking for.puddinman13– puddinman132015-06-24 20:04:47 +00:00Commented Jun 24, 2015 at 20:04
-
look at this question maybe is what are you looking for stackoverflow.com/questions/11287484/…Daniel Gpe Reyes– Daniel Gpe Reyes2015-06-24 21:11:19 +00:00Commented Jun 24, 2015 at 21:11
Add a comment
|
1 Answer
It's hard to tell exactly what you're asking, but if you need to use a string from client-side JavaScript in your server side C#, you can always POST back to an action of your controller with Ajax.
JavaScript:
var testString = "Hello, World!"
$.ajax({
type: "POST",
url: "/Test/TestAction",
data: testString,
success: function (data) {
// write your function which will call on success
}});
And in your controller...
public class TestController : Controller
{
public ActionResult Test(string id)
{
// id is "Hello, World!"
// do whatever you want with that string here
}
}