Advertisement
Guest User

Untitled

a guest
Jun 28th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. public class HomeController : Controller
  2. {
  3. public ActionResult Index()
  4. {
  5. var list = new SelectList(new[]
  6. {
  7. new { ID = "1", Name = "Select Id" },
  8. new { ID = "2", Name = "1001" },
  9. new { ID = "3", Name = "1002" },
  10. new { ID = "4", Name = "1003" },
  11. },
  12. "ID", "Name", 1);
  13.  
  14. ViewData["list"] = list;
  15.  
  16. return View();
  17. }
  18.  
  19. public List<Customer> GetCustomerById(String CustomerId)
  20. {
  21. var objCustomer = new List<Customer>();
  22.  
  23. objCustomer.Add(new Customer {CustomerId="1001", FirstName="John", LastName="Travolta", EmailAddress="John.Travolta@yahoo.com"});
  24. objCustomer.Add(new Customer {CustomerId = "1002", FirstName = "Bayo", LastName = "Adenuga", EmailAddress = "Bayo.Adenuga@yahoo.com" });
  25. objCustomer.Add(new Customer { CustomerId = "1003", FirstName = "Feyi", LastName = "Olawoye", EmailAddress = "feyi.olawoye@yahoo.com" });
  26.  
  27.  
  28. var objResult = from c in objCustomer
  29. where c.CustomerId == CustomerId
  30. select c;
  31.  
  32. return objResult.ToList();
  33. }
  34.  
  35. [HttpPost]
  36. public ActionResult Action(string customerId)
  37. {
  38. var results = GetCustomerById(customerId);
  39. return Json(results);
  40. }
  41.  
  42. }
  43.  
  44. @model DropdownSelectedFillTextBox.Models.Customer
  45.  
  46. @{
  47. ViewBag.Title = "Home Page";
  48. }
  49.  
  50. <script src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js">
  51. </script>
  52.  
  53. @using (@Html.BeginForm("Action","Home",FormMethod.Post))
  54. {
  55.  
  56. <h2>Dotuns Test</h2>
  57.  
  58. <div>
  59. @Html.DropDownList("drpList",ViewData["list"] as SelectList)
  60. </div>
  61.  
  62. <div>First Name</div>
  63. <div>
  64. @Html.TextBox("FirstName", null, new { @class = "form-control" })
  65. </div>
  66.  
  67. <div>Last Name</div>
  68. <div>
  69. @Html.TextBox("LastName", null, new { @class = "form-control" })
  70. </div>
  71.  
  72. <div>Email Address</div>
  73. <div>
  74. @Html.TextBox("EmailAddress", null, new { @class = "form-control" })
  75. </div>
  76.  
  77. }
  78.  
  79. <script type="text/javascript">
  80.  
  81. function Action(customerId) {
  82. $.ajax({
  83. url: '@Url.Action("Action", "Home")',
  84. type: "POST",
  85. data: { "customerId": customerId },
  86. "success": function (data) {
  87. if (data != null) {
  88. var vdata = data;
  89. $("#FirstName").val(vdata[0].FirstName);
  90. $("#LastName").val(vdata[0].LastName);
  91. $("#EmailAddress").val(vdata[0].EmailAddress);
  92. }
  93. }
  94. });
  95. }
  96. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement