0
I’m trying to save a change I try to make by selecting a Category in a Dropdownlistfor
.
I keep following the Debug and the View is sending the selected Category, but I am not able to continue this change.
My Subcategory Controller looks like this:
// GET: SubCategorias/Edit/5
public ActionResult Edit(Guid? id)
{
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
SubCategoriaViewModel subCategoriaViewModel =
_subCategoriaAppService.ObterPorId(id.Value);
subCategoriaViewModel.Categorias = _categoriaAppService.ObterTodas();
if (subCategoriaViewModel == null)
return HttpNotFound();
return View(subCategoriaViewModel);
}
// POST: SubCategorias/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(SubCategoriaViewModel subCategoriaViewModel)
{
_subCategoriaAppService.Atualizar(subCategoriaViewModel);
return RedirectToAction("Index");
}
My method for upgrading is 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 the way I’m trying to persist this http://imgur.com/a/i75n3
How is the subcategory structureViewModel ? Add to question.
– LP. Gonçalves
@LP.Gonçalves I updated the question
– Iago Frota
Iago Frota, post the solution of the problem and then mark your answer as accepted (I believe it releases only two days later). So future users who are having an equal or similar problem know what to do.
– LP. Gonçalves