How to pass the @Html.Dropdownlist value to the controller?

Asked

Viewed 300 times

0

I have a registration modal that I receive 2 values, I use an AJAX to save the information, however the value of @Html.DropDownList does not go to my Controller, the strange is the value of the Description arrives in the controller.

<div class="modal-body">
            <div class="form-horizontal">
                @Html.ValidationSummary(true)

                <div class="form-group">
                    @Html.Label("Descrição", new { @class = "control-label col-md-3" })
                    <div class="col-md-9">
                        @Html.TextBoxFor(model => model.Descricao, new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.Descricao)
                    </div>
                </div>
                <div class="form-group">
                    @Html.Label("Tipo de Cálculo", new { @class = "control-label col-md-3" })
                    <div class="col-md-9">
                        @Html.DropDownList("TipoCalculo")
                    </div><br />
                </div>

            </div>
        </div>

<script>
$(document).ready(function () {       
    //$("#AjaxPost").click(function () {
    //alert($('#TipoCalculo :selected').val())

    $("#AjaxPost").click(function () {
        var dataObject = {
            Descricao: $("#Descricao").val(),
            TipoCalculo: $('#TipoCalculo :selected').text(),
        };

        $.ajax({
            url: "@Url.Action("SalvarMilestone", "Dashboard")",
            type: "POST", 
            data: dataObject,
            dataType: "json"
        });
    });
});

inserir a descrição da imagem aqui

  • Hello, I recreated your scenario and it worked normally. How is your model class? Does it have any validation for the Tipocalculo field? How you are populating the values of the Type field?

  • @Bernardobotelho, yes it is populating... I used the following code to see if it appeared Alert stating the value of the combo and it appears normal $("#AjaxPost").click(function () {&#xA; //alert($('#TipoCalculo :selected').val())

  • I saw that your commented code uses val() to verify the value, but at the time of sending you used text(). The Dropbox values have text and value?

  • Yes, it has the two

  • Try putting date:{Milestone:dataObject}; to pass the data

No answers

Browser other questions tagged

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