There is no ViewData item of type 'IEnumerableSelectListItem' that has the key 'SearchToners'. But the DropDownList is NOT within the Form

0

I have this form:

@using (Html.BeginForm("Create", "Equipments", FormMethod.Post, new { }))
                    {
                @Html.AntiForgeryToken()
                @Html.HiddenFor(model => model.Parts)
                @Html.HiddenFor(model => model.Toners)
                <div class="box-body">
                    <div class="form-group">
                        @Html.LabelFor(model => model.SerialNumber)
                        @Html.EditorFor(model => model.SerialNumber, new { htmlAttributes = new { @class = "form-control", placeholder = "Entre serial del equipo", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.TypeMachine)
                        @Html.EnumDropDownListFor(model => model.TypeMachine, "Seleccione...", htmlAttributes: new { @class = "form-control", required = "required" })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.Model)
                        @Html.EditorFor(model => model.Model, new { htmlAttributes = new { @class = "form-control", placeholder = "Entre modelo del equipo", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.Location)
                        @Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control", placeholder = "Entre ubicación del equipo", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.IP)
                        <div class="input-group">
                            <div class="input-group-addon">
                                <i class="fa fa-desktop"></i>
                            </div>
                            <input class="form-control text-box single-line" data-val="true" data-val-length="Debe ser una dirección IP válida" data-val-length-max="12" data-val-length-min="4" data-val-regex="Debe ser una dirección IP válida" data-val-regex-pattern="^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" data-val-required="Dirección IP requerida" id="IP" name="IP" placeholder="172.239.17.192" type="text" required>
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.PMInterval)
                        @Html.EditorFor(model => model.PMInterval, new { htmlAttributes = new { @class = "form-control", placeholder = "60,000", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.CounterBnWCopies)
                        @Html.EditorFor(model => model.CounterBnWCopies, new { htmlAttributes = new { @class = "form-control", placeholder = "60,000", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.CounterBnWPrints)
                        @Html.EditorFor(model => model.CounterBnWPrints, new { htmlAttributes = new { @class = "form-control", placeholder = "60,000", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.CounterColorCopies)
                        @Html.EditorFor(model => model.CounterColorCopies, new { htmlAttributes = new { @class = "form-control", placeholder = "60,000", required = "required" } })
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.CounterColorPrints)
                        @Html.EditorFor(model => model.CounterColorPrints, new { htmlAttributes = new { @class = "form-control", placeholder = "60,000", required = "required" } })
                    </div>

                    <div class="form-group ">
                        <div class="checkbox">
                            <input type="checkbox" name="IsActive" id="IsActive" data-val="true" data-val-required="El campo Disponible / Activo es obligatorio." style="position: absolute; opacity: 0;">
                            <label for="IsActive"> Disponible / Activo</label>
                        </div>
                        <p class="help-block">Este Equipo estará disponible?</p>
                    </div>
                </div>
                <div class="box-footer">
                    <input type="submit" class="btn btn-primary pull-right" value="Crear">
                </div>
            }

and also, this element:

@Html.DropDownList("SearchToners", ViewBag.SearchToners as IEnumerable<SelectListItem>, "Buscar Toners para modelo", new { @class = "form-control selectpicker", style = "width:270px" })

Method in the controller

    public ActionResult Create()
    {
        ViewBag.BreadCrumb = new List<string> { "Stock", "Equipos", "Crear" };
        ViewBag.SearchToners = new SelectList(EquipmentService.ToList().ToList(), "Id", "Model");
        return View();
    }

The DropDownList is out and far from the Form. I do not know why I get that error if it's not even within request that it was sent to the controller.

    
asked by Dionny Prensa 05.01.2017 в 21:27
source

1 answer

0

[SOLVED]

In the post method I am returning the view without more. Then ASP, intelligently, reads the view to which the server's response will be addressed.

When ASP tried to draw the DropDownList it could not find the ViewBag.SearchToner. It does not exist, because it was never believed, in that response from the server.

Methods:

    [HttpGet]
    public ActionResult Create()
    {
        ViewBag.SearchToners = new SelectList(EquipmentService.ToList().ToList(), "Id", "Model");
        return View();
    }

Before the solution:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(string test)
    {
        return View();
    }

After the solution:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(string test)
    {
        ViewBag.SearchToners = new SelectList(EquipmentService.ToList().ToList(), "Id", "Model");
        return View();
    }
    
answered by 05.01.2017 в 21:45