2
I am trying to register a Subcategory that needs a Category. I have Subcategoriaviewmodel, where I created the fields public IEnumerable<CategoriaViewModel> Categorias { get; set; } and public Guid CategoriaId { get; set; }. Tbm have public virtual Categoria Categoria { get; set; } pro EF. 
My question is: how I would implement for me to receive in my Controller the Category selected in View?
In my Viewmodel have it:
public class SubCategoriaViewModel
    {
        public SubCategoriaViewModel()
        {
        }
        [Key]
        public Guid SubCategoriaId { get; set; }
        [Required(ErrorMessage = ("Preencha o nome da SubCategoria."))]
        [MaxLength(60, ErrorMessage = ("Máximo {0} caracteres."))]
        [MinLength(1, ErrorMessage = ("Mínimo {0} caracteres."))]
        [DisplayName("Nome")]
        public string SubCategoriaNome { get; set; }
        public IEnumerable<CategoriaViewModel> Categorias { get; set; }
        public Guid CategoriaId { get; set; }
        //[ScaffoldColumn(false)]
        //public DomainValidation.Validation.ValidationResult ValidationResult { get; set; }
        public virtual Categoria Categoria { get; set; }
        //public ICollection<Produto> Produtos { get; set; }
    }
In my View have it:
<div class="form-group">
            @Html.LabelFor(model => model.Categoria, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @* Este aqui tá dando certo aparecer na tela, mas não imagino um implementação para pegar um item selecionado *@
                @Html.DropDownListFor(model => model.CategoriaId,
                 new SelectList(Model.Categorias, "CategoriaId", "CategoriaNome"), new { @class = "form-control" })
                @* Esse aqui é so pra teste *@
                @Html.DropDownListFor(model => model.CategoriaId,
                 new SelectList(Model.Categorias, "CategoriaId", "CategoriaNome"), ((IEnumerable < Categorias)), new { @class = "form-control" })
            </div>
        </div>
In my Controller of the Subcategory I have this:
 // GET: SubCategorias/Create
        public ActionResult Create()
        {
            subCategoriaViewModel.Categorias = _categoriaAppService.ObterTodas();
            return View(subCategoriaViewModel);
        }
        // POST: SubCategorias/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(
            SubCategoriaViewModel subCategoriaViewModel)
        {
            subCategoriaViewModel =
                _subCategoriaAppService
                .Adicionar(subCategoriaViewModel);
            return View(subCategoriaViewModel);
        }
Your model doesn’t come filled out when you post it? Your model.Categoriaid contains the chosen value in the dropdown.
– LP. Gonçalves
@LP.Gonçalves You were really right. I was already with what I wanted, but I have another problem. When I add a new subcategory, the
Dropdwonlistis empty. To fix this, I need to get off this screen and then go back to her.– Iago Frota
@LP.Gonçalves Como faço para o
Dorpdwonliststay populated?– Iago Frota
sorry for the delay, you managed ?
– LP. Gonçalves
@LP.Gonçalves No problems. I still can’t get.
– Iago Frota