Html.BeginForm give me a exception when I add a range sliders code

153 views Asked by At

I'm working with a big form using Html.BeginForm in my asp.net app with MVC 5. Without add the las part of my code, everything is ok, but when I add the last form-group with various range slider, it give me a exception in a DropDownList, I was searching 2 days about this problem , it means what is sending the select List with null value, but it only happens when I add the last part of code, so I suppose its ok.

This is my View

@{ ViewBag.title = "Usuario";}

@using (Html.BeginForm("Create", "Users", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <div class="row">
                            <div class="col-lg-6">
                                <h2>Datos Personales</h2>
                            </div>
                            <div class="col-lg-6">
                                <div class="form-group">
                                    <div class="col-sm-5">
                                        <button type="button" class="btn btn-primary btn-rounded">
                                            Consultar Logs
                                        </button>
                                    </div>
                                    <div class="col-sm-3">
                                        <button type="button" class="btn btn-primary btn-rounded">
                                            Cancelar
                                        </button>
                                    </div>
                                    <div class="col-sm-3">
                                        <button type="submit" value="Send" class="btn btn-primary btn-rounded">
                                            Guardar
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="ibox-content">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    Cliente<sup style="color:red">*</sup>
                                </label>
                                <div class="col-sm-10">
                                    @Html.DropDownListFor(model => model.CustomersId, (SelectList)ViewData["Customers"], "-- Seleccione un cliente --", new { @style = "width:400px; text-align: left", @class = "btn btn-default dropdown-toggle" })
                                    @Html.ValidationMessageFor(model => model.CustomersId)
                                </div>
                            </div>
                            @*Recogida de datos para el metodo create*@
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    Nombre<sup style="color:red">*</sup>
                                </label>
                                <div class="col-sm-4">
                                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                                </div>
                                <label class="col-sm-2 control-label">
                                    Apellidos
                                </label>
                                <div class="col-sm-4">
                                    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    Email
                                </label>
                                <div class="col-sm-4">
                                    @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                                </div>
                                <label class="col-sm-2 control-label">
                                    Teléfono
                                </label>
                                <div class="col-sm-4">
                                    @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
                                </div>
                            </div>
                        </div>
                        @*Hasta aqui*@
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h2>Alta Usuario</h2>
                    </div>
                    <div class="ibox-content">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    Usuario Inactivo
                                    @Html.EditorFor(model => model.InactiveUser, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.InactiveUser, "", new { @class = "text-danger" })

                                </label>
                                <label class="col-sm-2 control-label">
                                    Fecha Alta<sup style="color:red">*</sup>
                                </label>
                                <div class="col-sm-2">

                                    @Html.EditorFor(model => model.EntryDate, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.EntryDate, "", new { @class = "text-danger" })

                                </div>
                                <label class="col-sm-2 control-label">
                                    Fecha Baja<sup style="color:red">*</sup>
                                </label>
                                <div class="col-sm-2">

                                    @Html.EditorFor(model => model.LeavingDate, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.LeavingDate, "", new { @class = "text-danger" })

                                </div>
                            </div>
                            <div class="form-group" style="display:flex">
                                <label class="col-sm-1 control-label">
                                    Tipo<sup style="color:red">*</sup>
                                </label>
                                <div class="col-sm-3">
                                    @Html.DropDownListFor(model => model.Type, (SelectList)ViewData["VehiculeType"], "- Seleccionar -")
                                    @Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
                                </div>


                                @*Al seleccionar en el select Vehículo aparece lo siguiente*@
                                <div class="sel oculto">
                                    <label class="col-sm-2 control-label">
                                        Matrícula
                                    </label>
                                    <div class="col-sm-3">

                                        @Html.EditorFor(model => model.CarNumber, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.CarNumber, "", new { @class = "text-danger" })

                                    </div>
                                    <label class="col-sm-2 control-label">
                                        Capacidad Cisterna
                                    </label>
                                    <div class="col-sm-3">

                                        @Html.EditorFor(model => model.TankCapacity, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.TankCapacity, "", new { @class = "text-danger" })

                                    </div>
                                </div>

                                @*Esta opcion aparece solo si en el select NO se elige vehículo*@
                                <div class="sel2">
                                    <label class="col-sm-3 control-label">
                                        Límite Suministro
                                    </label>
                                    <div class="col-sm-3">

                                        @Html.EditorFor(model => model.SupplyLimit, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.SupplyLimit, "", new { @class = "text-danger" })

                                    </div>
                                </div>
                                @*Hasta aquí*@
                            </div>


                            <div class="form-group">
                                <label class="col-sm-2">
                                    Estaciones Permitidas
                                </label>
                            </div>
                            <div class="form-group">

                                @*Obtener todos los checkbox y label de base de datos*@
                                <div class="col-sm-6">
                                    @foreach (var loc in ViewBag.DLocations)
                                    {
                                        <label class="col-sm-5 control-label checkLocations">@loc.Name</label>
                                        <div class="col-sm-1 checkLocations">
                                            <input type="checkbox" />
                                        </div>
                                    }
                                </div>

                                <div class="col-sm-6">
                                    @foreach (var loc in ViewBag.BLocations)
                                    {
                                        <label class="col-sm-5 control-label checkLocations">@loc.Name</label>
                                        <div class="col-sm-1 checkLocations">
                                            <input type="checkbox" />
                                        </div>
                                    }
                                </div>
                                @*Hasta aquí*@

                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    Obervaciones<sup style="color:red">*</sup>
                                </label>
                                <div class="col-sm-10">
                                    @Html.TextAreaFor(model => model.Comments,
                                             new { cols = "75", rows = "10", htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
                                </div>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>




        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title"><h2>Acceso App DIBEmasa</h2></div>
                    <div class="ibox-content" style="text-align:center">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">
                                    CUIU
                                </label>
                                <div class="col-sm-2">
                                    @Html.EditorFor(model => model.CUIU, new { htmlAttributes = new { @class = "form-control" } })
                                    @Html.ValidationMessageFor(model => model.CUIU, "", new { @class = "text-danger" })
                                </div>
                                <div class="col-sm-3">
                                    <label class="control-label">
                                        Visualización:<sup style="color:red">*</sup>
                                    </label>
                                    <div>
                                        <label>
                                            Dársena
                                            @Html.EditorFor(model => model.VisualizationDarsena, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.VisualizationDarsena, "", new { @class = "text-danger" })
                                        </label>
                                        <label>
                                            Baldeo
                                            @Html.EditorFor(model => model.VisualizationBaldeo, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.VisualizationBaldeo, "", new { @class = "text-danger" })
                                        </label>
                                    </div>
                                </div>
                                <div class="col-sm-3">
                                    <label class="control-label">
                                        Actuación:<sup style="color:red">*</sup>
                                    </label>
                                    <div>
                                        <label>
                                            Dársena
                                            @Html.EditorFor(model => model.ActionDarsena, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.ActionDarsena, "", new { @class = "text-danger" })
                                        </label>
                                        <label>
                                            Baldeo
                                            @Html.EditorFor(model => model.ActionBaldeo, new { htmlAttributes = new { @class = "form-control" } })
                                            @Html.ValidationMessageFor(model => model.ActionBaldeo, "", new { @class = "text-danger" })
                                        </label>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>



        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title"><h2>Horarios</h2></div>
                    <div class="ibox-content">
                        <div class="form-horizontal">
                            <div class="form-control horarios">
                                <label class="col-sm-2 control-label">
                                    Lunes <input type="checkbox" />
                                </label>
                                <div class="col-sm-10">
                                    <input type="text" class="js-range-slider1" name="Monday" value="" 
                                           data-type="double"
                                           data-min="0"
                                           data-max="24"
                                           data-from="2"
                                           data-to="20"
                                           data-grid="true"
                                           style="background-color:blue" />
                                    <div>
                                        <input type="text" id="old_from" />
                                        @Html.EditorFor(model => model.StartWorkingMonday, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.StartWorkingMonday, "", new { @class = "text-danger" })
                                    </div>

                                    <div>
                                        <input type="text" id="old_to" />
                                        @Html.EditorFor(model => model.FinishWorkMonday, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.FinishWorkMonday, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                         </div>
                     </div>
                 </div>
              </div>



    </div>

} <!-- End BeginForm-->



This is my controller

 // GET: User/Create
        public ActionResult Create()
        {
            var data = _con.GetAllCustomers();
            ViewData["Customers"] = new SelectList(data, "id", "Name"); //value, text  
            var VehiculeType = new[] {
                new SelectListItem { Text = "Vehículo", Value = "Vehículo" },
                new SelectListItem { Text = "Superusuario", Value = "Superusuario" },
                new SelectListItem { Text ="Baldeo", Value = "Baldeo"},
                };
                               
            ViewData["VehiculeType"] = new SelectList(VehiculeType, "Text", "Value");

            var DLocations = _con.DarsenaLocations();
            var BLocations = _con.BaldeoLocations();
            ViewBag.DLocations = DLocations;
            ViewBag.BLocations = BLocations;


            return View();
        }             

        // POST: User/Create
        [HttpPost]
        public ActionResult Create(User user)
        {
           
            try
            {
                if (ModelState.IsValid)
                {
                    var data = _con.GetAllCustomers();
                    ViewData["Customers"] = new SelectList(data, "id", "Name"); //value, text  

                    _con.CreateUser(user);
                    return RedirectToAction("Index");
                }               
            }
            catch (Exception ex)
            {
                
            }
            return View(user);
        }

And this is the error when I submit the form

System.InvalidOperationException: 'The ViewData item that has the key 'CustomersId' is of type 'System.Int32' but must be of type 'IEnumerable'.' Error when submit the form

0

There are 0 answers