Select inside a dropdown using jquery

Asked

Viewed 266 times

2

I can’t get the script to work. I have to do research into the second Dropdown (Scams) and save the ID of the second dropdown to make the Post the ID in the BD

Controller: This Controller will seek the dioceses of the first dropdown.

public ActionResult GetDioceses()
        {
            //  Obter todas as dioceses usando Entity Framework e LINQ 
            var dioceses = db.Diocese
                .Select(d => new { d.DioceseID, d.Nome })
                .OrderBy(d => d.Nome);
            if (HttpContext.Request.IsAjaxRequest())          
                return Json(new SelectList(
                           dioceses,
                           "DioceseID",
                           "Nome"), JsonRequestBehavior.AllowGet
                           );     
            return View(dioceses);
        }

This controller will collect all the Vigararias belonging to the diocese.

public ActionResult GetVigarariasByDioceses(int DioceseId)
        {
            // Obter Vigararias de uma Diocese utilizando LINQ.
            var vigararias = db.Vigararia
                .Where(v => v.DioceseID == DioceseId)
                .Select(v => new { v.VigarariaID, v.Nome })
                .OrderBy(v => v.Nome);

            if (HttpContext.Request.IsAjaxRequest())
                return Json(new SelectList(
                                vigararias,
                                "VigarariaID",
                                "Nome"), JsonRequestBehavior.AllowGet
                            );

            return View(vigararias);
        }

View:

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $.getJSON("@Url.Action("GetDioceses", "Paroquia")"
                var items = "<option>---------------------</option>";
                $.each(data, function (i, diocese) {
                    items += "<option value='" + diocese.Value + "'>" + diocese.Text + "</option>";
                });
                $("#Dioceses").html(items);
            });

            $("#Dioceses").change(function () {
                 $.getJSON("@Url.Action("GetVigarariasByDioceses", "Paroquia")/"+ $("#Dioceses > option:selected").attr("value"), function (data) {
                    var items = "<option>---------------------</option>";
                    $.each(data, function (i, vigararia) {
                        items += "<option value='" + vigararia.Value + "'>" + vigararia.Text + "</option>";
                    });
                    $("#Vigararias").html(items);
                });
            });
        });
    </script>
 <div class="form-group">

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()


                        <div class="form-group">
                            @Html.Label("Dioceses", htmlAttributes: new { @class = "control-label col-md-2" })

                            <div class="col-md-3">
                                <select id="Dioceses" name="Dioceses" class="form-control"></select>
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.Label("Vigararias", htmlAttributes: new { @class = "control-label col-md-2" })                              
                            <div class="col-md-3">
                                <select id="Vigararias" name="Vigararias" class="form-control"></select>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="col-md-offset-2 col-md-10">
                                <input type="submit" value="Criar" class="btn btn-primary" />
                            </div>
                        </div>
    }

1 answer

2

Apparently everything is ok. Use @Url.Action to mount jQuery addresses:

$.getJSON("@Url.Action("GetDioceses", "MeuController")", function (data) { ... }

$.getJSON("@Url.Action("GetVigariasByDioceses", "MeuController")/" + $("#Dioceses > option:selected").val(), function (data) { ... }

EDIT

If $.getJSON() not working, use $.get() or $.ajax().

  • Shows items from the first dropdown to not from the second dropdown.

  • He even put breakpoints on Controller? Revised the generated HTML code?

  • In the controller he does not get to enter the method "Getvigarariasbydioceses", I’ve reviewed the view and can not identify the error.

  • Script name was wrong but it still doesn’t work.

  • *Method name was wrong not script.

  • Did you mark any of the methods with [HttpPost]? The right thing is [HttpGet]. The spelling of the elements is correct?

  • Spelling of the elements? what is the meaning . Sorry

  • If the names of Actions are correctly written.

  • Yes everything is correct. the problem I think is here. '$. getJSON("@Url.Action("Getvigarariasbydioceses", "Parochia")/" + $("#Dioceses > option:Selected"). attr("value"), Function (date) {...}', because it never gets to enter the method "Getvigarariasbydioceses"

  • Will I need AJAX?

  • I edited the reply with suggestions.

  • With the get continues to only work the first dropdown. If I reformulate to just one function will give? is that it never gets to go to the second function.

  • Must be some other problem. The code apparently is ok.

  • I gave up and reworked the code, but I have another problem.http://answall.com/questions/108464/problema-a-fazer-a-query-dentro-da-segunda-dropdown

Show 9 more comments

Browser other questions tagged

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