1
Have two models we map that will work like this : The model Afazer will have a Combobox(Dropdownlist) which will make available the categories for the user to select, but these categories are registered by the same, so in the source I have something like this :
public class Afazer
{
public int Id { get; set; }
[Required(ErrorMessage = "Digite a titulo!")]
[MinLength(1, ErrorMessage = "O tamanho mínimo da titulo são 1 caracteres.")]
[StringLength(30, ErrorMessage = "O tamanho máximo são 30 caracteres.")]
[Display(Name = "Titulo: ")]
public string AFZ_TITULO { get; set; }
[Required(ErrorMessage = "Digite a descrição!")]
[MinLength(5, ErrorMessage = "O tamanho mínimo da descrição são 5 caracteres.")]
[StringLength(256, ErrorMessage = "O tamanho máximo são 256 caracteres.")]
[Display(Name = "Descrição: ")]
public string AFZ_DESCRICAO { get; set; }
[Display(Name = "Nivel de Prioridade")]
[Range(1, int.MaxValue, ErrorMessage = "Selecione um Nivel valido!")]
public Prioridades AFZ_NIVEL { get; set; }
[Display(Name = "Categoria: ")]
public virtual ICollection<Categorias> Categoria { get; set; }
[Display(Name = "Status: ")]
public bool AFZ_DONE { get; set; }
[Browsable(false)]
public string ApplicationUserId { get; set; }
}
public class Categorias
{
public int Id { get; set; }
[Required(ErrorMessage = "Digite a categoria!")]
[MinLength(1, ErrorMessage = "O tamanho mínimo da categoria são 1 caracteres.")]
[StringLength(15, ErrorMessage = "O tamanho máximo são 15 caracteres.")]
[Display(Name = "Titulo: ")]
public string CAT_DESCRICAO { get; set; }
public string ApplicationUserId { get; set; }
}
Already in my View I tried something like this:(But it doesn’t work)
@model Teste.Models.Afazer
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Afazer</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="form-group">
@Html.LabelFor(model => model.Categoria, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("CAT_DESCRICAO", new SelectList(Model.Categoria.AsEnumerable()))
@Html.ValidationMessageFor(model => model.Categoria, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Great explanation, you’re awesome!
– Leonardo
Thanks! I’m glad it worked out!
– Leandro Simões