jeudi 27 avril 2017

Checkbox updates index mvc

Good afternoon everyone, I have an Index in my view that displays a table of restaurants (local in spanish). I added a checkbox to the view, I need the checkbox to filter the index while checked, if not then to show the complete index. I don't know if there is a way to do this with mvc,

this is my view

   @model IEnumerable<AutoPlanMCV.Models.Local>

<div class="row">
    <table class="col-md-12">
        <tr>
            <th>Nuevo</th>
            <th>Instaldo</th>
            <th>Capacitar</th>
        </tr>
        <tr>
            <td>@Model.Count(x => x.Estado.State == "Nuevo")</td>
            <td>@Model.Count(x=> x.Estado.State =="Instalado")</td>
            <td>@Model.Count(x=>x.Estado.State == "Capacitar")</td>
        </tr>
            </table>
</div>

<div class="row">
    <label for="verBajas">Ver Bajas</label>
    <input type="checkbox" name="verBajas" value="true" id="verBajas"/>

</div>

<div>   
    <p>
        @Html.ActionLink("Crear nuevo Local", "Create")
        <br />

        @Html.ActionLink("Export to Excel", "ExportToExcel")
    </p>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="panel panel-primary list-panel" id="list-panel">
            <div class="panel-heading list-panel-heading">
                <h1 class="panel-title list-panel-title">POS PDS UY</h1>


            </div>
            <div class="panel-body">
                <table id="assets-data-table" class="table table-striped table-bordered" style="width:100%">
                    <thead>
                        <tr>
                            <th>V.Id</th>
                            <th>Id</th>
                            <th>Comercio</th>
                            <th>Direccion</th>
                            <th>Telefono</th>
                            <th>Ingreso</th>
                            <th>Provincia</th>
                            <th>Estado</th>
                            <th>Bonificado</th>
                            <th>Premium</th>
                            <th>Gestionar</th>

                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var asset in Model)
                        {

                            <tr>
                                <td>@asset.ViejoId</td>
                                <td>@asset.NuevoId</td>
                                <td>@asset.NombreComercio</td>
                                <td>@asset.Direccion</td>
                                <td>@asset.Telefono</td>
                                <td>@Html.DisplayFor(modelItem => asset.FechaInstalacion)</td>
                                <td>@asset.Provincia</td>
                                <td>@asset.Estado.State</td>
                                @if (asset.Bonificado == true)
                                {
                                    string i = "Bonificado";
                                    <td>@i</td>
                                }
                                else
                                {
                                    string i = "No";
                                    <td>@i</td>
                                }

                                @if (asset.Premium == true)
                                {
                                    string i = "Premium";
                                    <td>@i</td>
                                }
                                else
                                {
                                    string i = "No";
                                    <td>@i</td>
                                }

                                <td>
                                    @Html.ActionLink("Edit", "Edit", new { id = asset.Id ,estadoid = asset.Estado.Id,proveedorid = asset.Proveedor.Id}) |
                                    @Html.ActionLink("Details", "Details", new { id = asset.Id }) |
                                    @Html.ActionLink("Delete", "Delete", new { id = asset.Id })|
                                    @Html.ActionLink("Comment", "AgregarComentario", new { id = asset.Id })
                                </td>
                                                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

@section Scripts
{

    <script type="text/javascript">
     $(document).ready(function () {

         $('#assets-data-table').DataTable();
     });
    </script>

}

this is the actionResult in my controller

public ActionResult IndexAsset(bool verBajas = false)
    {


       var locales = db.Locales.Include(l => l.Estado).Include(l => l.Proveedor).Where(l => l.Proveedor.Nombre == "PDSUY");

        if(verBajas)
        {
            locales = db.Locales.Include(l => l.Estado).Include(l => l.Proveedor).Where(l => l.Proveedor.Nombre == "PDSUY").Where(l => l.Estado.State != "Bajas");
        } else if (verBajas == false)
        {
            locales = db.Locales.Include(l => l.Estado).Include(l => l.Proveedor).Where(l => l.Proveedor.Nombre == "PDSUY");
        }



        return View(locales.ToList());
    }

If I dont initialize "verBajas" as false, I get a null error, even though I set the value in the view as true.

Any Ideas are appreciated.




Aucun commentaire:

Enregistrer un commentaire