In this example I will show you how to bind viewbag value to an html control using jQuery in mvc.
Example:
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="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#txtMessage").val('@ViewBag.Message');
});
</script>
<h2>can we bind viewbag to htmlcontrol using jquery</h2>
<input type="text" id="txtMessage" />
<br />
it is working fine.
<br />
Description:
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 asp.net razor view. While running the application, on the pageLoad, the string message is binding on the textbox.
Output:
Video
Post your comments / questions
Recent Article
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
- Remove all special characters, punctuation except spaces from string - Python
Related Article