Step 1: Create a new ASP.NET MVC Empty Project using Visual Studio.
This example shows that Best 5 Ways To Set Default In ASP.NET MVC
Add new Layout.cshtml into shared folder. Add references of Kendo, CSS, and JavaScript into this Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<title>Best 5 Ways to 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("Best5ForIndia.Blogspot.in", "", "")</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 - My Telerik MVC Application</p>
</div>
</div>
</footer>
</body>
</html>
Step 2: Create a model as bellow
public class Person
{
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 int PinCode { get; set; }
}
Step 3: In HomeController for Action Method Index pass new object of our model.
public class HomeController : Controller
{
public ActionResult Index()
{
TempData["name"] = "Rupesh Kahane";
ViewData["address"] = "Pune";
ViewBag.pinCode = "411027";
return View(new Set_Default_Values.Models.Person());
}
}
Step 4: Create View for our Action Method & write bellow code into it.
@model Set_Default_Values.Models.Person
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2 style="margin-left:15%">Best 5 Ways To Set Default 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 Name using ViewData :
@Html.TextBox("Address", @ViewData["address"])
<br />
Set Default Name using ViewBag :
@Html.TextBox("PinCode", (string)ViewBag.pinCode)
<script>
$(document).ready(function () {
$("input[id='Id']").val(1000);
});
</script>
Step 5: Now Run the application & you will get result as
This example shows that Best 5 Ways To Set Default In ASP.NET MVC
Add new Layout.cshtml into shared folder. Add references of Kendo, CSS, and JavaScript into this Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<title>Best 5 Ways to 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("Best5ForIndia.Blogspot.in", "", "")</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 - My Telerik MVC Application</p>
</div>
</div>
</footer>
</body>
</html>
Step 2: Create a model as bellow
public class Person
{
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 int PinCode { get; set; }
}
Step 3: In HomeController for Action Method Index pass new object of our model.
public class HomeController : Controller
{
public ActionResult Index()
{
TempData["name"] = "Rupesh Kahane";
ViewData["address"] = "Pune";
ViewBag.pinCode = "411027";
return View(new Set_Default_Values.Models.Person());
}
}
Step 4: Create View for our Action Method & write bellow code into it.
@model Set_Default_Values.Models.Person
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<h2 style="margin-left:15%">Best 5 Ways To Set Default 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 Name using ViewData :
@Html.TextBox("Address", @ViewData["address"])
<br />
Set Default Name using ViewBag :
@Html.TextBox("PinCode", (string)ViewBag.pinCode)
<script>
$(document).ready(function () {
$("input[id='Id']").val(1000);
});
</script>
Step 5: Now Run the application & you will get result as
Summary
In this article, you learned the basics of Best 5 Ways To Set Default In ASP.NET MVC
This comment has been removed by a blog administrator.
ReplyDelete