How To Set Default In ASP.NET MVC

Step 1

Create a new ASP.NET MVC Empty Project using Visual Studio.
 
This example shows how to set Default values in ASP.NET MVC

Add new Layout.cshtml into shared folder. Add references of Kendo, CSS (which shows an UI in proper manner) and JavaScript into this Layout.cshtml
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.     <title>Set Default Values In MVC</title>  
  5.     <link href="~/Content/Site.css" rel="stylesheet" />  
  6.     <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />  
  7.     <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />  
  8.     <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />  
  9.     <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.dataviz.default.min.css" rel="stylesheet" type="text/css" />  
  10.     <script src="http://cdn.kendostatic.com/2014.2.716/js/jquery.min.js"></script>  
  11.     <script src="http://cdn.kendostatic.com/2014.2.716/js/kendo.all.min.js"></script>  
  12.     <script src="http://cdn.kendostatic.com/2014.2.716/js/kendo.aspnetmvc.min.js"></script>  
  13.     <script src="~/Scripts/kendo.modernizr.custom.js"></script>  
  14. </head>  
  15. <body>  
  16.     <header>  
  17.         <div class="content-wrapper">  
  18.             <div class="float-left">  
  19.                 <p class="site-title" style="color:#ff0000">@Html.ActionLink("www.CoderFunda.com", "", "")</p>  
  20.             </div>  
  21.             <div class="float-right">  
  22.                 <nav>  
  23.                     <ul id="menu">  
  24.                         <li>@Html.ActionLink("Home", "Index", "Home")</li>  
  25.                         <li>@Html.ActionLink("About", "About", "Home")</li>  
  26.                         <li>@Html.ActionLink("Contact", "Contact", "Home")</li>  
  27.                     </ul>  
  28.                 </nav>  
  29.             </div>  
  30.         </div>  
  31.     </header>  
  32.     <div id="body">  
  33.         @RenderSection("featured", required: false)  
  34.         <section class="content-wrapper main-content clear-fix">  
  35.             @RenderBody()  
  36.         </section>  
  37.     </div>  
  38.   
  39.     <footer>  
  40.         <div class="content-wrapper">  
  41.             <div class="float-left">  
  42.                 <p>© @DateTime.Now.Year - www.CoderFunda.com</p>  
  43.             </div>  
  44.         </div>  
  45.     </footer>  
  46. </body>  
  47. </html>  
Step 2

Create a Model with properties as below and create a constructor in which I am going to set a default value to Gender. 
  1. public class Person  
  2.    {  
  3.        public Person()  
  4.        {  
  5.           Gender = "1";  
  6.        }  
  7.        private DateTime _SetDefaultDate = DateTime.Now;  
  8.   
  9.        public DateTime UserDefaultDate  
  10.        {  
  11.            get  
  12.            {  
  13.                return _SetDefaultDate;  
  14.            }  
  15.            set  
  16.            {  
  17.                _SetDefaultDate = value;  
  18.            }  
  19.        }  
  20.   
  21.        public int Id { getset; }  
  22.        public string Name { getset; }  
  23.        public string Address { getset; }  
  24.        public string WebSite { getset; }  
  25.        public string Gender { getset; }         
  26.    }  
Step 3

Now, create a Home Controller which contains an Action Method where I am going to set some values using TempDate, ViewBag, ViewData. In Home Controller, for Action Method Index, pass new object of the Model which will help to assign an date using Model to UI.
  1. public ActionResult Index()  
  2.         {  
  3.             TempData["name"] = "Rupesh Kahane";  
  4.             ViewData["address"] = "Pune";  
  5.             ViewBag.webSite = "www.CoderFunda.com";  
  6.             return View(new Set_Default_Values.Models.Person());  
  7.         }  
Step 4

Create View for your Action Method and write the below code into it. 
  1. @model Set_Default_Values.Models.Person  
  2. @{  
  3.     ViewBag.Title = "Index";  
  4.     Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }  
  6.   
  7. <h2 style="margin-left:15%">Set Default Values In MVC</h2>  
  8.   
  9. <br />  
  10. Set Default Date using Model :  
  11. @Html.TextBoxFor(model => model.UserDefaultDate)  
  12. <br />  
  13. Set Default Id using Javascript :  
  14. @Html.TextBoxFor(model => model.Id)  
  15. <br />  
  16. Set Default Name using TempData :  
  17. @Html.TextBox("Name", @TempData["name"])  
  18. <br />  
  19. Set Default Address using ViewData :  
  20. @Html.TextBox("Address", @ViewData["address"])  
  21. <br />  
  22. Set Default Web Site using ViewBag :  
  23. @Html.TextBox("WebSite", (string)ViewBag.webSite)  
  24. <br />  
  25. Set Default Drop Down List value using Model Constructor:  
  26. @Html.DropDownListFor(model => model.Gender,(new List<SelectListItem>() {  
  27.                           new SelectListItem() {  
  28.                               Text = "Male",  
  29.                               Value = "1"  
  30.                           },  
  31.                           new SelectListItem() {  
  32.                               Text = "Female",  
  33.                               Value = "2"  
  34.                           }  
  35.                         }),"Select Gender")  
  36.   
  37. <script>  
  38.     $(document).ready(function () {  
  39.         $("input[id='Id']").val(1000);  
  40.     });  
  41. </script>  
Step 5

Now, run the application and you will get the results below:

 
 
Summary

In this blog, you learned the basics of how to set default in ASP.NET MVC.