Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum Airlines
- {
- Unknown = 0,
- BritishAirways = 1,
- VirginAtlantic = 2,
- AirFrance = 3
- }
- @model Enum
- @foreach (var value in Enum.GetValues(Model.GetType()))
- {
- @Html.RadioButtonFor(m => m, value)
- @Html.Label(value.ToString())
- }
- @model MvcTest.Models.Airlines
- @foreach (var value in Enum.GetValues(typeof(MvcTest.Models.Airlines)))
- {
- @Html.RadioButtonFor(m => m, value)
- @Html.Label(value.ToString())
- }
- public class TestModel
- {
- [Required(ErrorMessage = "select one item")]
- public Airlines Airline { get; set; }
- }
- public class HomeController : Controller
- {
- [HttpGet]
- public ActionResult Index()
- {
- return View(new TestModel());
- }
- [HttpPost]
- public ActionResult Index(TestModel model)
- {
- if (ModelState.IsValid)
- {
- return RedirectToAction("Index");
- }
- return View(model);
- }
- }
- @model MvcTest.Models.TestModel
- @{
- ViewBag.Title = "Index";
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>Index</h2>
- @using (Html.BeginForm())
- {
- @Html.EditorFor(m => m.Airline)
- <input type="submit" value="Submit" />
- @Html.ValidationSummary(false)
- }
- @model Enum
- @foreach (var value in Enum.GetValues(Model.GetType()))
- {
- var id = TagBuilder.CreateSanitizedId(string.Format(
- "{0}_{1}_{2}", ViewData.TemplateInfo.HtmlFieldPrefix, Model.GetType(), value));
- <div>
- @Html.RadioButton(string.Empty, value, value.Equals(Model), new { id })
- @Html.Label(value.ToString(), new { @for = id })
- </div>
- }
- @Html.EditorFor(m => m.Airline, "EnumRadioButtonList")
- @Html.RadioButtonFor(x => x.Airlines, Airlines.Unknown)
- @Html.RadioButtonFor(x => x.Airlines, Airlines.BritishAirways)
- @Html.RadioButtonFor(x => x.Airlines, Airlines.VirginAtlantic)
- @Html.RadioButtonFor(x => x.Airlines, Airlines.AirFrance)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement