Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. public enum Airlines
  2. {
  3. Unknown = 0,
  4. BritishAirways = 1,
  5. VirginAtlantic = 2,
  6. AirFrance = 3
  7. }
  8.  
  9. @model Enum
  10. @foreach (var value in Enum.GetValues(Model.GetType()))
  11. {
  12. @Html.RadioButtonFor(m => m, value)
  13. @Html.Label(value.ToString())
  14. }
  15.  
  16. @model MvcTest.Models.Airlines
  17. @foreach (var value in Enum.GetValues(typeof(MvcTest.Models.Airlines)))
  18. {
  19. @Html.RadioButtonFor(m => m, value)
  20. @Html.Label(value.ToString())
  21. }
  22.  
  23. public class TestModel
  24. {
  25. [Required(ErrorMessage = "select one item")]
  26. public Airlines Airline { get; set; }
  27. }
  28.  
  29. public class HomeController : Controller
  30. {
  31. [HttpGet]
  32. public ActionResult Index()
  33. {
  34. return View(new TestModel());
  35. }
  36.  
  37. [HttpPost]
  38. public ActionResult Index(TestModel model)
  39. {
  40. if (ModelState.IsValid)
  41. {
  42. return RedirectToAction("Index");
  43. }
  44.  
  45. return View(model);
  46. }
  47. }
  48.  
  49. @model MvcTest.Models.TestModel
  50. @{
  51. ViewBag.Title = "Index";
  52. Layout = "~/Views/Shared/_Layout.cshtml";
  53. }
  54. <h2>Index</h2>
  55. @using (Html.BeginForm())
  56. {
  57. @Html.EditorFor(m => m.Airline)
  58. <input type="submit" value="Submit" />
  59. @Html.ValidationSummary(false)
  60. }
  61.  
  62. @model Enum
  63. @foreach (var value in Enum.GetValues(Model.GetType()))
  64. {
  65. var id = TagBuilder.CreateSanitizedId(string.Format(
  66. "{0}_{1}_{2}", ViewData.TemplateInfo.HtmlFieldPrefix, Model.GetType(), value));
  67. <div>
  68. @Html.RadioButton(string.Empty, value, value.Equals(Model), new { id })
  69. @Html.Label(value.ToString(), new { @for = id })
  70. </div>
  71. }
  72.  
  73. @Html.EditorFor(m => m.Airline, "EnumRadioButtonList")
  74.  
  75. @Html.RadioButtonFor(x => x.Airlines, Airlines.Unknown)
  76. @Html.RadioButtonFor(x => x.Airlines, Airlines.BritishAirways)
  77. @Html.RadioButtonFor(x => x.Airlines, Airlines.VirginAtlantic)
  78. @Html.RadioButtonFor(x => x.Airlines, Airlines.AirFrance)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement