Step oneCreate a new ASP.NET MVC Empty Projection using Visual Studio.
This instance 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 style) 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.mutual.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.two.716/styles/kendo.default.min.css" rel = "stylesheet" blazon = "text/css" />
- < link href = "http://cdn.kendostatic.com/2014.2.716/styles/kendo.dataviz.default.min.css" rel = "stylesheet" blazon = "text/css" />
- < script src = "http://cdn.kendostatic.com/2014.2.716/js/jquery.min.js" > </ script >
- < script src = "http://cdn.kendostatic.com/2014.ii.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 >
- </ caput >
- < body >
- < header >
- < div class = "content-wrapper" >
- < div class = "float-left" >
- < p class = "site-championship" 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", "Most", "Domicile") </ li >
- < li > @Html.ActionLink("Contact", "Contact", "Home") </ li >
- </ ul >
- </ nav >
- </ div >
- </ div >
- </ header >
- < div id = "body" >
- @RenderSection("featured", required: imitation)
- < section class = "content-wrapper master-content clear-gear up" >
- @RenderBody()
- </ department >
- </ div >
-
- < footer >
- < div course = "content-wrapper" >
- < div class = "bladder-left" >
- < p > © @DateTime.Now.Twelvemonth - www.CoderFunda.com </ p >
- </ div >
- </ div >
- </ footer >
- </ trunk >
- </ html >
Stride 2Create a Model with backdrop every bit beneath 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
- {
- become
- {
- render _SetDefaultDate;
- }
- set
- {
- _SetDefaultDate = value;
- }
- }
-
- public int Id { get ; set ; }
- public string Name { get ; prepare ; }
- public string Accost { become ; set up ; }
- public string WebSite { get ; set ; }
- public string Gender { get ; set ; }
- }
Step threeNow, create a Dwelling Controller which contains an Activeness Method where I am going to prepare some values using TempDate, ViewBag, ViewData. In Home Controller, for Activeness Method Alphabetize, pass new object of the Model which will help to assign an appointment 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());
- }
Pace ivCreate View for your Activity 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 Appointment using Model :
- @Html.TextBoxFor(model = > model.UserDefaultDate)
- < br />
- Fix Default Id using Javascript :
- @Html.TextBoxFor(model = > model.Id)
- < br />
- Prepare Default Name using TempData :
- @Html.TextBox("Name", @TempData["name"])
- < br />
- Fix Default Address using ViewData :
- @Html.TextBox("Address", @ViewData["address"])
- < br />
- Fix Default Web Site using ViewBag :
- @Html.TextBox("WebSite", (string)ViewBag.webSite)
- < br />
- Gear up Default Drop Down List value using Model Constructor:
- @Html.DropDownListFor(model = > model.Gender,(new List < SelectListItem > () {
- new SelectListItem() {
- Text = "Male" ,
- Value = "i"
- },
- new SelectListItem() {
- Text = "Female person" ,
- Value = "2"
- }
- }),"Select Gender")
-
- < script >
- $(document).ready(office () {
- $("input[id = 'Id' ]").val(1000);
- });
- </ script >
Footstep 5Now, run the awarding and you will become the results below:
Summary
In this weblog, you lot learned the basics of how to set default in ASP.Net MVC.
0 Response to "How To Assign Data To Textboxfor To C# Mvc"
Post a Comment