0

I'm new to c# and asp.net MVC and i'm working on a small project. I'm having an error on a create view using a view model. Whats happening is that after I complete the form when I submit it the data I populated isn't being received by the controller. Below is the code for my view, for my create on the controller and a view of the ModelView i'm using

 //Controllers        
// GET: Producto/Create
public ActionResult Create()
{
    return View(ProductoViewModel.CargarListas());
}


// POST: Producto/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ProductoViewModel producto)
{
    Producto aux = producto.Producto;
    aux.IdRecetaPreferida = producto.IdRecetaPreferida;
    if (aux.Guardar())
    {
        return RedirectToAction(nameof(Index));
    }
    return View();
}

/*
* My view model im using on the view
*/
public class ProductoViewModel
{
    public Producto Producto {get;set;}
    [Display(Name = "Receta")]
    public long IdRecetaPreferida { get; set; }
    public Receta Receta { get; set; }
    public List<Receta> Recetas { get; set; }

    public ProductoViewModel()
    {
        this.Recetas = new List<Receta>();
        this.Receta = new Receta();
    }

    public static ProductoViewModel CargarListas()
    {
        ProductoViewModel productoViewModel = new ProductoViewModel()
        {
            Recetas = Receta.TraerTodos()
        };
        return productoViewModel;
    }

}

//My create view
@model WebApp.ViewModels.ProductoViewModel

<h2>Ingresar Producto</h2>

<hr />
<div class="row">
    <div class="col-md-12">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group row">
                <label asp-for="Producto.Nombre" class="col-sm-2 col-form-label"></label>
                <div class="col-sm-10">
                    <input asp-for="Producto.Nombre" class="form-control" placeholder="Nombre Producto" />
                    <span asp-validation-for="Producto.Nombre" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group row">
                <label asp-for="Producto.Descripcion" class="col-sm-2 col-form-label"></label>
                <div class="col-sm-10">
                    <input asp-for="Producto.Descripcion" class="form-control" placeholder="Una rica descripcion del producto" />
                    <span asp-validation-for="Producto.Descripcion" class="text-danger"></span>
                </div>
            </div>
            <div class="form-group row">
                <label asp-for="IdRecetaPreferida" class="col-sm-2 col-form-label"></label>
                <div class="col-sm-10">
                    @Html.DropDownListFor(m => m.IdRecetaPreferida, new SelectList(Model.Recetas, "Id", "Nombre"), new { @class = "custom-select" })
                </div>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
                <a asp-action="Index">Volver</a>
            </div>
        </form>
    </div>
</div>

If could point out my error would appreciate it. Below is a screenshot of the variable being debugged

Debug view of the create in the controller

elcharrua
  • 1,582
  • 6
  • 30
  • 50
  • 1
    have you taken the time to step thru the code using the debugger..? – MethodMan Dec 03 '17 at 14:42
  • @MethodMan I've been debugging and narrowed it down. I think the problem is the drop down thats making things not work. What I did is comment the drop down and the data in being passed correctly. What I can't figure out is whats wrong with the dropdown – elcharrua Dec 03 '17 at 20:08
  • View models do not contain data models! (just the properties of your data models that you need in the view) –  Dec 03 '17 at 20:53

1 Answers1

0

I believe you forgot to specify that the form should send a POST request instead of a GET (default).

<form asp-action="Create" method="post">

Or you can remove the HttpPost attribute from the Create method.

Haytam
  • 4,643
  • 2
  • 20
  • 43