Dropdown Cascata

Asked

Viewed 409 times

3

My problem is passing values to controller when making an update.

I can do the Cascading all right. Follow my code.

First DropDown:

@Html.DropDownList(
    "idCanalIndicadoresMassivo", 
    ViewBag.idCanalIndicadoresMassivo as SelectList, 
    "[Selecione]", new { id = "CanalMassivo" }
);

As soon as I get the value of the first loads the second.

@Html.DropDownList(
    "idSegmento", 
    new SelectList(Enumerable.Empty<SelectListItem>()),
    "[Selecione]", new { id = "Segmento", disabled = "disabled" }
);

Man controller is like this.

ViewBag.idSegmento = new SelectList(db.Segmento, "idSegmento", "descricao", indicadoresmassivosp.idSegmento);

To carry the second I carry so.

private IList<Segmento> GetClasses(int idCanalMassivo)
{
    return db.Segmento.Where(m => m.idCanalIndicadoresMassivo == idCanalMassivo).ToList();
}

public JsonResult GetStates(string CanalMassivo)
{
    var classesList = this.GetClasses(Convert.ToInt32(CanalMassivo));
    var classesData = classesList.Select(m => new SelectListItem()
    {
        Value = m.idCanalIndicadoresMassivo.ToString(),
    });

    var states = classesData.Select(m => m.Text).ToList();
    return Json(states, JsonRequestBehavior.AllowGet);
}

This is my Javascript:

<script type="text/javascript">
    $(function () {
        $('#CanalMassivo').on('change', function () {
            var stateDropdown = $('#Segmento');
            //disable state drop down
            stateDropdown.prop('disabled', 'disabled');
            //clear drop down of old states
            stateDropdown.empty();

            var select = $("#Segmento");
            select.empty();
            select.append($('<option/>', {
                value: 0,
                text: "[Selecione]"
            }));

            //retrieve selected country
            var CanalMassivo = $(this).val();
            if (CanalMassivo.length > 0) {
                // retrieve data using a Url.Action() to construct url
                $.getJSON('@Url.Action("GetStates")', { CanalMassivo: CanalMassivo })
                    .done(function (data) {
                        //re-enable state drop down
                        stateDropdown.removeProp('disabled');
                        //for each returned state
                        $.each(data, function (i, state) {
                            //Create new option
                            var option = $('<option />').html(state);
                            //append state states drop down
                            stateDropdown.append(option);
                        });
                    })
                .fail(function (jqxhr, textStatus, error) {
                    var err = textStatus + ", " + error;
                    console.log("Request Failed: " + err);
                });
            }
        });
    })
</script>

Controller that saves

[HttpPost]
    public ActionResult Edit(IndicadoresMassivoSP indicadoresmassivosp)
    {
        {
            if (ModelState.IsValid)
            {

                db.Entry(indicadoresmassivosp).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return ViewOrPartial(indicadoresmassivosp);
        }
    }

Model

[Required(ErrorMessageResourceType = typeof(App_GlobalResources.Global), ErrorMessageResourceName = "Required")]
    [Display(ResourceType = typeof(App_GlobalResources.Global), Name = "IndicadoresMassivoSP_idIndicadoresMassivo")]        
    public int idIndicadoresMassivo { get; set; }

    [Required(ErrorMessageResourceType = typeof(App_GlobalResources.Global), ErrorMessageResourceName = "Required")]
    [Display(ResourceType = typeof(App_GlobalResources.Global), Name = "IndicadoresMassivoSP_idIndicadoresMassivoMensal")]      
    public int idIndicadoresMassivoMensal { get; set; }


    [Display(ResourceType = typeof(App_GlobalResources.Global), Name = "IndicadoresMassivoSP_idSegmento")]      
    public Nullable<int> idSegmento { get; set; }

    public virtual IndicadoresMassivoMensal IndicadoresMassivoMensal { get; set; }
    public virtual Segmento Segmento { get; set; }

Then when I give the update it does not locate the value of the second DropDown, I don’t know if my question was confused. I need to receive values in the controller

I do not know if the way I am doing is also right because I took several examples and came out of this, in case there is another example that I can follow.

  • Can you please enter the code for Action who receives the POST in his Controller?

  • Gypsy, I added that’s what I wanted ?

  • Yes, but I can’t see the properties of Model. I’m gonna need the Model also.

  • I added the Model eliminated some fields because it has a lot, and that needs ?

  • Yes, now I can answer.

  • English/Portuguese code strange kkkk

Show 1 more comment

1 answer

2


Prefer to use the typed version of the extension:

@Html.DropDownListFor(model => model.idCanalIndicadoresMassivo, 
    ViewBag.idCanalIndicadoresMassivo as SelectList, 
    "[Selecione]", new { id = "CanalMassivo" }
);

And:

@Html.DropDownListFor(model => model.idSegmento, 
    new SelectList(Enumerable.Empty<SelectListItem>()),
    "[Selecione]", new { id = "Segmento", disabled = "disabled" }
);

Apparently it’s a problem of Binding.

  • Perfect.! I will add one more question related to the code could help me ?

  • 1

    @Edgararaujo Open a new question, please.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.