Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. using System.Collections.Generic;
  2. namespace UI.Models.Forms
  3. {
  4. public class TempListModel : ContentModel
  5. {
  6. public TempListModel()
  7. {
  8. Comment = new List<string>();
  9. }
  10. public IList<string> Comment { get; set; } //Comments for each URL in the list
  11. }
  12. }
  13.  
  14. @model UI.Models.Forms.TempListModel
  15. @using (Html.BeginForm("temptest", "Test", new { id = 1 }, FormMethod.Post, new { id = "listForm", name = "listForm" }))
  16. {
  17. <ul>
  18. @for (int i = 0; i < Model.Comment.Count(); i++)
  19. {
  20. <li>
  21. <div class="llformlabel">
  22. Notes:
  23. <div>@Model.Comment[i]</div>
  24. @Html.TextArea("Comment", Model.Comment[i], 4, 63, new { @id = "Comment_" + i, @title = "Comment" })</div>
  25. </li>
  26. }
  27. </ul>
  28. <input type="submit" value="Save Changes" />
  29. }
  30.  
  31. using System.Collections.Generic;
  32. using System.Web.Mvc;
  33. using UI.Models.Forms;
  34. namespace UI.Controllers
  35. {
  36. public class TestController : Controller
  37. {
  38. [AcceptVerbs(HttpVerbs.Post)]
  39. public ActionResult TempTest(TempListModel model)
  40. {
  41. //This function executes after the user submits the form.
  42. //If server side validation fails then the user should be shown the form as it was when they submitted.
  43. //model.Comment = GetComments(); //In my real world example this comes from a database.
  44. if (true) //!ModelState.IsValid) //In my real world code this is a validation step that may fail
  45. {
  46. return View(model);
  47. }
  48. }
  49. [AcceptVerbs(HttpVerbs.Get)]
  50. public ActionResult TempTest(int? id)
  51. {
  52. //In the real world example there is a lot going on in this function.
  53. //It is used to load data from databases and set up the model to be displayed.
  54. var model = new TempListModel();
  55. model.Comment = GetComments();
  56. return View("TempTest", "TempLayout", model);
  57. }
  58. private static IList<string> GetComments()
  59. {
  60. //Simple sample function used for demo purposes.
  61. IList<string> comments = new List<string>();
  62. comments.Add("Comment 1");
  63. comments.Add("Comment 2");
  64. comments.Add("Comment 3");
  65. return comments;
  66. }
  67. }
  68. }
  69.  
  70. [AcceptVerbs(HttpVerbs.Post)]
  71. public ActionResult TempTest(TempListModel model)
  72. {
  73. if (ModelState.IsValid)
  74. {
  75. return RedirectToAction("TempTest");
  76. }
  77. return View(model);
  78. }
  79.  
  80. @for (int i = 0; i < Model.Comment.Count(); i++)
  81. {
  82. <li>
  83. @Html.TextAreaFor(m => m.Comment[i], 4, 63, new { @title = "Comment" })
  84. </li>
  85. }
  86.  
  87. @Html.TextArea("Comment", Model.Comment[i], 4, 63, new { @id = "Comment_" + i, @title = "Comment" })
  88.  
  89. @Html.TextAreaFor(m => m.Comment[i], 4, 63, new { @title = "Comment" })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement