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
- <!DOCTYPE html>
- <html>
- <head>
- <title>Set Default Values In MVC</title>
- <link href="~/Content/Site.css" rel="stylesheet" />
- <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.common.min.css" rel="stylesheet" type="text/css" />
- <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.dataviz.min.css" rel="stylesheet" type="text/css" />
- <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.default.min.css" rel="stylesheet" type="text/css" />
- <link href="http://cdn.kendostatic.com/2014.2.716/styles/kendo.dataviz.default.min.css" rel="stylesheet" type="text/css" />
- <script src="http://cdn.kendostatic.com/2014.2.716/js/jquery.min.js"></script>
- <script src="http://cdn.kendostatic.com/2014.2.716/js/kendo.all.min.js"></script>
- <script src="http://cdn.kendostatic.com/2014.2.716/js/kendo.aspnetmvc.min.js"></script>
- <script src="~/Scripts/kendo.modernizr.custom.js"></script>
- </head>
- <body>
- <header>
- <div class="content-wrapper">
- <div class="float-left">
- <p class="site-title" style="color:#ff0000">@Html.ActionLink("www.CoderFunda.com", "", "")</p>
- </div>
- <div class="float-right">
- <nav>
- <ul id="menu">
- <li>@Html.ActionLink("Home", "Index", "Home")</li>
- <li>@Html.ActionLink("About", "About", "Home")</li>
- <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
- </ul>
- </nav>
- </div>
- </div>
- </header>
- <div id="body">
- @RenderSection("featured", required: false)
- <section class="content-wrapper main-content clear-fix">
- @RenderBody()
- </section>
- </div>
-
- <footer>
- <div class="content-wrapper">
- <div class="float-left">
- <p>© @DateTime.Now.Year - www.CoderFunda.com</p>
- </div>
- </div>
- </footer>
- </body>
- </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.
- public class Person
- {
- public Person()
- {
- Gender = "1";
- }
- private DateTime _SetDefaultDate = DateTime.Now;
-
- public DateTime UserDefaultDate
- {
- get
- {
- return _SetDefaultDate;
- }
- set
- {
- _SetDefaultDate = value;
- }
- }
-
- public int Id { get; set; }
- public string Name { get; set; }
- public string Address { get; set; }
- public string WebSite { get; set; }
- public string Gender { get; set; }
- }
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.
- public ActionResult Index()
- {
- TempData["name"] = "Rupesh Kahane";
- ViewData["address"] = "Pune";
- ViewBag.webSite = "www.CoderFunda.com";
- return View(new Set_Default_Values.Models.Person());
- }
Step 4
Create View for your Action Method and write the below code into it.
- @model Set_Default_Values.Models.Person
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
-
- <h2 style="margin-left:15%">Set Default Values In MVC</h2>
-
- <br />
- Set Default Date using Model :
- @Html.TextBoxFor(model => model.UserDefaultDate)
- <br />
- Set Default Id using Javascript :
- @Html.TextBoxFor(model => model.Id)
- <br />
- Set Default Name using TempData :
- @Html.TextBox("Name", @TempData["name"])
- <br />
- Set Default Address using ViewData :
- @Html.TextBox("Address", @ViewData["address"])
- <br />
- Set Default Web Site using ViewBag :
- @Html.TextBox("WebSite", (string)ViewBag.webSite)
- <br />
- Set Default Drop Down List value using Model Constructor:
- @Html.DropDownListFor(model => model.Gender,(new List<SelectListItem>() {
- new SelectListItem() {
- Text = "Male",
- Value = "1"
- },
- new SelectListItem() {
- Text = "Female",
- Value = "2"
- }
- }),"Select Gender")
-
- <script>
- $(document).ready(function () {
- $("input[id='Id']").val(1000);
- });
- </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.