Enter a screen with the dropdown option already selected

Asked

Viewed 44 times

0

I got this dropdown

<div class="form-group">
        @Html.Label("Grupo de Desconto", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GrupoDescontos", new SelectList(ViewBag.Desconto, "Id", "Descricao"), new { @id = "GrupoDescontos", @class = "Makewide" })
            @*@Html.ValidationMessageFor(model => model.HomeTemplateId, "", new { @class = "text-danger" })*@
        </div>
    </div>

Whenever I select an ID on the list screen and when I enter the Detail screen, the ID=1 option is selected, even if I have selected Section 3, for example. How do I leave selected in Dropdown the corresponding ID item?

EDIT1

This is cshtml with the dropdown list that is called by index and then by Dropdown

<div>
    <h4></h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.Id)

    <div class="form-group">
        @Html.Label("Grupo de Desconto", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("GrupoDescontos", new SelectList(ViewBag.Desconto, "Id", "Descricao"), new { @id = "GrupoDescontos", @class = "Makewide" })
            @*@Html.ValidationMessageFor(model => model.HomeTemplateId, "", new { @class = "text-danger" })*@
        </div>
    </div>

</div>

<div id="GridPartial"></div>

@Html.Partial("DetailsPartial")

<p>
    @Html.ActionLink("Back to List", "Index")
</p>

<script type="text/javascript">
    $(document).ready(function () {
        $("#GrupoDescontos").change(function () {
            $.ajax
            ({
                url: '' + $(this).val(),
                type: 'GET',
                    success: function (dados) {
                    $("#GridPartial").empty();
                    $("#GridPartial").html(dados);
                    //var resultado = dados;
                },
                    error: function (erro) {

                        alert("erro");

                }
            });
        });
    });

    $(document).ready(function () {
        $("#GrupoDescontos").val();
    });
</script>
  • Since there was no cshtml before I made an example by putting jquery to run in a $( Document ).ready, but you are using ajax, if it is after his call you want change you can make the call in ajax Success.

1 answer

1


You can use a simple Jquery like this:

$("#GrupoDescontos").val("Valor_a_Selecionar");

If you bring that kind of value ViewBag:

$("#GrupoDescontos").val('@ViewBag.Valor_a_Selecionar');

See an example below:

$( document ).ready(function() {
    $("#MeuDropDownList").val("2");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="MeuDropDownList">
  <option value="0">Selecione...</option>
  <option value="1">Azul</option>
  <option value="2">Vermelho</option>
  <option value="3">Verde</option>
</select>

With C# in Controller

If you prefer you can do it with the Selected of SelectListItem:

List<SelectListItem> list = new List<SelectListItem> {
    new SelectListItem() { Text = "Selecione", Value = "0" },
    new SelectListItem() { Text = "Azul", Value = "1" },
    new SelectListItem() { Text = "Vermelho", Value = "2", Selected = true },
    new SelectListItem() { Text = "Verde", Value = "3" }
};
  • val() in your example is fixed, as I would pass a dynamic val() according to the selected item?

  • It can be by the model, it can be a viewbag... if you mount your select list in the controller you can already set which value is selected.

  • But how would I take that amount and play inside jquery?

  • @pnet updated the answer with one more example

  • A Viewbag inside the val() of jquery with me gives error

  • @pnet the examples I gave you will need to use in your ajax’s Success, selecting the value after the completion of the ajax because it is updating the Gridpartial with the return of the Ajax... vc tbm can do in the controller that the ajax makes the call not to be necessary to make another logic in Success...

  • All that’s left is to clean up the DIV and I can go home happy

Show 2 more comments

Browser other questions tagged

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