I had previously asked a question that thanks to Davlio has already been resolved.
However, another question arose, I have two stored procedures mapped in entityframework, one to fill dropdownlist
(GetDepartamentos_Result) and another to fill webgrid
depending on the selection you make in the dropdownlist (SearchEmployeesDropDown_Result).
The question is that from what I have read you can not have two models in a view so I made a new model that my two models of my stored procedure contain, leaving as follows:
namespace WebgridPagingSortingFiltering.Models
{
public class ViewModels
{
public List<GetDepartamentos_Result> Combo { get; set; }
public List<SearchEmployeesDropDown_Result> Grid { get; set; }
public int SelectedDeparmentID { get; set; }
}
}
add a new property SelectedDeparmentID
for the value of the Id of dropdownlist
.
In my controller I have the following two methods one to fill the dropdownlist
and another one to populate the wegrid
:
To fill drop
:
public List<GetDepartamentos_Result> GetDepartamentos()
{
using (TigerEntities db = new TigerEntities())
{
ViewModels vm = new ViewModels();
vm.Combo = db.GetDepartamentos().ToList();
return vm.Combo;
}
}
To fill webgrid
:
public List<SearchEmployeesDropDown_Result> GetEmpleadosbyDrop (int Id_dep)
{
using (TigerEntities db = new TigerEntities())
{
ViewModels vm = new ViewModels();
vm.Grid = db.SearchEmployeesDropDown(Id_dep).ToList();
return vm.Grid;
}
}
And I have an actionresult method that loads the data in the view for dropdownlist
:
public ActionResult Combo()
{
var a = GetDepartamentos();
ViewBag.Departamentos = new SelectList(a, "Id_Departamento",
"Nombre_Departamento");
return View(b);
}
In my view I have the following:
@model WebgridPagingSortingFiltering.Models.ViewModels
@{
Layout = null;
var grid = new WebGrid(canPage: true, rowsPerPage: 4);
grid.Bind(source: Model.Grid, rowCount: ViewBag.TotalRows);
}
@using (Html.BeginForm("combo", "home", FormMethod.Post, new { @class =
"navbar-form navbar-left" }))
{
<div class="form-group">
@Html.DropDownListFor(model =>
model.SelectedDeparmentID, ViewBag.Departamentos as SelectList, new {
@class = "form-control" })
</div>
<button type="submit" value="Search" class="btn btn-
success">Search</button>
}
My question is how I pass the value of the dropdownlist's id to the controller so that it can filter with the stored procedure I have done and show me the data in the webgrid.
I have the same actionresult method but I do not know what to do as a parameter:
[HttpPost]
public ActionResult Combo(??) No se si pasarle el modelo ViewModels o
el SelectedDeparmentID
{
var a = GetDepartamentos();
ViewBag.Departamentos = new SelectList(a, "Id_Departamento",
"Nombre_Departamento");
var b = GetEmpleadosbyDrop(??);
return View(b);
}
I would greatly appreciate your help that you can provide me. Greetings