How to pass a DropDownList Value from a view to a controller using ViewModel?

0

I'm learning about ASP.NET Core and I've been a few days stuck in the following hopefully I could help.

In my controller I have the following '

 public IActionResult Index(string searchString)
 {
      ViewData["FiltroActual"] = searchString;

      var viewmodel = new ProductoCategoria(); //collecion de dos entidades para poder pasar dos viewmodels a la vista

      viewmodel.Productos = from p in _context.Producto
                           select p;

      viewmodel.Categorias = from c in _context.Categoria
                              select c;

        if (!String.IsNullOrEmpty(searchString))
        {
            viewmodel.Productos = viewmodel.Productos.Where(s => 
                   s.Titulo.ToUpper().Contains(searchString.ToUpper()));
        }
        return View(viewmodel);
    }

In the view I have the following

model VendoTest1.Models.ProductoIndexData
<form asp-action="Index" method="get" >
    <div class="col-sm-4" style="padding:0px 8px 0px 0px;">
        @Html.DropDownListFor(model => model.Categorias, new SelectList(Model.Categorias, "IdCategoria", "Nombre"), "-Categoria-")
     </div>
     <div class="col-sm-6">
        <div class="row">
            <input id="input-buscar-principal" class="col-xs-11" type="text" name="searchString" placeholder="que estas buscando?..." value="@ViewData["FiltroActual"]"/>
            <input type="submit" value="Buscar" class="btn btn-default" />
        </div>
     </div>
</form>

How can I send the id value of the dropdownlist to the controller?

    
asked by Daniel 26.04.2017 в 07:51
source

2 answers

1

As I can see, the action you execute is the Index , in asp-action you're telling it to go to the index when submitting, and the action Index is this:

public IActionResult Index(string searchString)
 {
      ViewData["FiltroActual"] = searchString;

      var viewmodel = new ProductoCategoria(); //collecion de dos entidades para poder pasar dos viewmodels a la vista

      viewmodel.Productos = from p in _context.Producto
                           select p;

      viewmodel.Categorias = from c in _context.Categoria
                              select c;

        if (!String.IsNullOrEmpty(searchString))
        {
            viewmodel.Productos = viewmodel.Productos.Where(s => 
                   s.Titulo.ToUpper().Contains(searchString.ToUpper()));
        }
        return View(viewmodel);
    }

That is the same action that runs the view.

Are you sure you are pointing to the indicated action? You are making a circular call, you are making the view call itself.

But if you want to send the id of the category, do this:

public IActionResult Index(string searchString, int? categorias)
 {
      ViewData["FiltroActual"] = searchString;

      var viewmodel = new ProductoCategoria(); //collecion de dos entidades para poder pasar dos viewmodels a la vista

      viewmodel.Productos = from p in _context.Producto
                           select p;

      viewmodel.Categorias = from c in _context.Categoria
                              select c;

        if (!String.IsNullOrEmpty(searchString))
        {
            viewmodel.Productos = viewmodel.Productos.Where(s => 
                   s.Titulo.ToUpper().Contains(searchString.ToUpper()));
        }
        return View(viewmodel);
    }

When you submit, then the action will receive the categories id.

    
answered by 26.04.2017 в 14:23
0

I give you an example

On the Controller

 public IActionResult Index(string Descripcion)
 {
 ...More code
 }

And on the View

 @using (Html.BeginForm("Index", "AquiElNombreDelControlador", new {},FormMethod.Get))
  {
    @Html.DropDownListFor(model => model.Categorias.Descripcion, new SelectList(Model.Categorias, "Descripcion", "Descripcion"), "-Categoria-")
  }

I hope that helps you

    
answered by 26.04.2017 в 10:22