MVC : using Json pass string value

4/1/2014·2 min read

This article try to show how to use Json post to post to server side, and return string result to client. It's not hard to set up, just need few steps. Hopefully, this will help your coding. So,in this example, let's say, you want to add some string value to your input data and return to your client side.

1. Create Server Side Code

So, first you could start create your server side code, add action method to your controller. Add HttpPost attribute to your method and paramter. You'll use paramter name and action name, when you call from client using Jquery post.

2. Add Html Control

In this example, you have text box, button and label. So, when user click button, get the value at textbox, add string value at server side and display the result to label.

3. How Html looks

4. Javascript(Jquery)

Here is important part, so when button click, you add event to your javascript file. Which add $.Post, and this'll use Jquery Post to calling server's controller, next would be your passing value, the last would be return data.

5. Latest Jquery Library

don't forget to add your jquery library.

6. Test various type

Looks like string is safe way to use for Json call, like int and decimal.