Last active
December 11, 2017 23:35
-
-
Save luisdeol/178b9c47b5016697a25817b8ff9b27e2 to your computer and use it in GitHub Desktop.
Exemplo DevMEdia
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Home.cshtml | |
@using WebApplication3.Controllers | |
@model WebApplication3.Controllers.Conta | |
@{ | |
ViewBag.Title = "Novidade do ASP.NET MVC 5"; | |
string nomeDiaSemana = Model.DiaDaSemana.GetDescription(); | |
} | |
<h2>HTML Helper EnumDropDownListFor()</h2> | |
@{ | |
} | |
@Html.DisplayFor(model => model.DiaDaSemanaString) | |
@Html.EnumDropDownListFor(model => model.DiaDaSemana, | |
"Selecione o Dia da Semana", new {@class="form-control"}) | |
---------------------------------------------------------------------------- | |
HomeController.cs | |
using System; | |
using System.ComponentModel; | |
using System.Reflection; | |
using System.Web.Mvc; | |
namespace WebApplication3.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
var conta = new Conta | |
{ | |
DiaDaSemana = Enumeradores.DiasDaSemana.Quinta, | |
DiaDaSemanaString = Enumeradores.DiasDaSemana.Quinta.GetDescription() | |
}; | |
return View(conta); | |
} | |
} | |
public class Enumeradores | |
{ | |
public enum DiasDaSemana | |
{ | |
[Description("Domingo")] | |
Domingo = 0, | |
[Description("Segunda-feira")] | |
Segunda = 1, | |
[Description("Terça-feira")] | |
Terca = 2, | |
[Description("Quarta-feira")] | |
Quarta = 3, | |
[Description("Quinta-feira")] | |
Quinta = 4, | |
[Description("Sexta-feira")] | |
Sexta = 5, | |
[Description("Sábado")] | |
Sabado = 6 | |
} | |
} | |
public static class Extensions | |
{ | |
public static string GetDescription(this Enum value) | |
{ | |
FieldInfo fi = value.GetType().GetField(value.ToString()); | |
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); | |
if (attributes.Length > 0) | |
{ | |
return attributes[0].Description; | |
} | |
return value.ToString(); | |
} | |
} | |
public class Conta | |
{ | |
public Enumeradores.DiasDaSemana DiaDaSemana { get; set; } | |
public string DiaDaSemanaString { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment