set viewbag in jquery

set viewbag in jquery, someone asked me to explain?

In this example I will show you how to bind viewbag value to an html control using jQuery in mvc. 


Step 1: Right click on the "Controllers" folder and add "Home" controller. Copy and paste the following code.

public ActionResult Index()
           ViewBag.Message = "hello I used JQuery";
            return View();

Step 2: Right click on the "Index" action method in the "HomeController" and add "Index" view. Copy and paste the following code.

<script src=""></script>
<script type="text/javascript">
   $(document).ready(function () {


<h2>can we bind viewbag to htmlcontrol using jquery</h2>
<input type="text" id="txtMessage" />
<br />

it is working fine.

<br />  


Here, I have assigned a message to the viewbag variable and create a text box and assign id as txtMessage and set viewbag in jQuery using razor view. While running the application, on the pageLoad, the string message is binding on the textbox.


set viewbag in jquery


Post your comments / questions