I'm trying to save a change I try to make by selecting a Category in a Dropdownlistfor
.
I'mfollowingtheDebugandtheViewissendingtheselectedCategory,butIcannotcontinuethischange.
MySubcategoryControllerlookslikethis:
//GET:SubCategorias/Edit/5publicActionResultEdit(Guid?id){if(id==null)returnnewHttpStatusCodeResult(HttpStatusCode.BadRequest);SubCategoriaViewModelsubCategoriaViewModel=_subCategoriaAppService.ObterPorId(id.Value);subCategoriaViewModel.Categorias=_categoriaAppService.ObterTodas();if(subCategoriaViewModel==null)returnHttpNotFound();returnView(subCategoriaViewModel);}//POST:SubCategorias/Edit/5[HttpPost][ValidateAntiForgeryToken]publicActionResultEdit(SubCategoriaViewModelsubCategoriaViewModel){_subCategoriaAppService.Atualizar(subCategoriaViewModel);returnRedirectToAction("Index");
}
My method for updating looks like this:
public SubCategoriaViewModel Atualizar(
SubCategoriaViewModel subCategoriaViewModel)
{
//var categoriaSelecionada =
// _categoriaService.ObterPorId(subCategoriaViewModel.CategoriaId);
subCategoriaViewModel.Categoria =
_categoriaService.ObterPorId(subCategoriaViewModel.CategoriaId);
var subCategoria =
Mapper.Map<SubCategoriaViewModel,
SubCategoria>(subCategoriaViewModel);
//subCategoria.Categoria = categoriaSelecionada;
_subCategoriaService.Atualizar(subCategoria);
return subCategoriaViewModel;
}
In my View :
<div class="form-group">
@Html.LabelFor(model => model.Categoria, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CategoriaId,
new SelectList(Model.Categorias, "CategoriaId", "CategoriaNome"), new { @class = "form-control" })
</div>
</div>
My ViewModel
public class SubCategoriaViewModel
{
public SubCategoriaViewModel()
{
SubCategoriaId = Guid.NewGuid();
}
[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 = new List<CategoriaViewModel>();
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; }
}
The interesting thing is that if I modify the name of the SubCategory it persisted! Anything, this is the way of how I'm trying to persist it link