banner



How To Assign Data To Textboxfor To C# Mvc

  • Rupesh Kahane
  • Updated date Mar 05, 2017
  • 80.4k
  • 0

In this weblog, y'all will learn how to set default in ASP.Internet MVC.

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

  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.mutual.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.two.716/styles/kendo.default.min.css" rel = "stylesheet" blazon = "text/css" />
  9. < link href = "http://cdn.kendostatic.com/2014.2.716/styles/kendo.dataviz.default.min.css" rel = "stylesheet" blazon = "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.ii.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. </ caput >
  15. < body >
  16. < header >
  17. < div class = "content-wrapper" >
  18. < div class = "float-left" >
  19. < p class = "site-championship" 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", "Most", "Domicile") </ 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: imitation)
  34. < section class = "content-wrapper master-content clear-gear up" >
  35.             @RenderBody()
  36. </ department >
  37. </ div >
  38. < footer >
  39. < div course = "content-wrapper" >
  40. < div class = "bladder-left" >
  41. < p > © @DateTime.Now.Twelvemonth - www.CoderFunda.com </ p >
  42. </ div >
  43. </ div >
  44. </ footer >
  45. </ trunk >
  46. </ 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.

  1. public class  Person
  2.    {
  3. public  Person()
  4.        {
  5.           Gender ="1" ;
  6.        }
  7. private  DateTime _SetDefaultDate = DateTime.Now;
  8. public  DateTime UserDefaultDate
  9.        {
  10. become
  11.            {
  12. render  _SetDefaultDate;
  13.            }
  14. set
  15.            {
  16.                _SetDefaultDate = value;
  17.            }
  18.        }
  19. public int  Id { get ; set ; }
  20. public string  Name { get ; prepare ; }
  21. public string  Accost { become ; set up ; }
  22. public string  WebSite { get ; set ; }
  23. public string  Gender { get ; set ; }
  24.    }

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.

  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.         }

Pace ivCreate View for your Activity 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. < h2 style = "margin-left:15%" > Set Default Values In MVC </ h2 >
  7. < br />
  8. Set Default Appointment using Model :
  9. @Html.TextBoxFor(model  = >  model.UserDefaultDate)
  10. < br />
  11. Fix Default Id using Javascript :
  12. @Html.TextBoxFor(model  = >  model.Id)
  13. < br />
  14. Prepare Default Name using TempData :
  15. @Html.TextBox("Name", @TempData["name"])
  16. < br />
  17. Fix Default Address using ViewData :
  18. @Html.TextBox("Address", @ViewData["address"])
  19. < br />
  20. Fix Default Web Site using ViewBag :
  21. @Html.TextBox("WebSite", (string)ViewBag.webSite)
  22. < br />
  23. Gear up Default Drop Down List value using Model Constructor:
  24. @Html.DropDownListFor(model  = >  model.Gender,(new List < SelectListItem > () {
  25.                           new SelectListItem() {
  26. Text  = "Male" ,
  27. Value  = "i"
  28.                           },
  29.                           new SelectListItem() {
  30. Text  = "Female person" ,
  31. Value  = "2"
  32.                           }
  33.                         }),"Select Gender")
  34. < script >
  35.     $(document).ready(office () {
  36.         $("input[id = 'Id' ]").val(1000);
  37.     });
  38. </ 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.

How To Assign Data To Textboxfor To C# Mvc,

Source: https://www.c-sharpcorner.com/Blogs/how-to-set-default-in-asp-net-mvc

Posted by: mcclurgyoughat.blogspot.com

0 Response to "How To Assign Data To Textboxfor To C# Mvc"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel