How to get the value of the DropDownList Id?

0

I am working with ASP.NET MVC 5, I am in need of getting the value of Id of Item selected from DropDownList , when I say value I refer to Id not to Descripción .

The DropDowList is loaded as follows: Controller

// GET: Proveedor/Create
    public ActionResult Create()
    {
        var proveedor = SdProveedor.ListaTipoDocumentoIdentidad();
        ViewBag.ListaProveedores = new SelectList(proveedor, "Id", "Descripcion");

        var model = new ProveedorDto();
        return View(model);
    }

View

<div class="form-group">
        @Html.LabelFor(model => model.DocumentoIdentidad, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("myDropDown", (SelectList)ViewBag.ListaProveedores)
        </div>
    </div>

Now I need to get the value of Id Selected from DropDownList , Would I have to create an event Changed or something similar that when you select a Item take the value and send it to the model to make the POST ?

    
asked by Pedro Ávila 18.01.2017 в 22:24
source

2 answers

1

In mvc the events do not exist, you have to make a post to the action Create so that the modelbinding assign the value to the property

but you should use

@Html.DropDownListFor(x=> x.ProveedorId, (SelectList)ViewBag.ListaProveedores)

In the DataProvider you have to define a property that assigns the value to the combo and that also defines the name with which it will map when it returns

Remember that modelbinding will look for matches between the name of the control and the property of the model that defines the action if your match assign the value

    
answered by 19.01.2017 / 12:28
source
1

You can try the following method to get the dropdownlist Id:

$('#tusDropDownList').change(function () {   
    /* Obtener el valor de tus dropdownlist */
    var selectedId = $(this).val();
});

I hope this helps ...

    
answered by 08.02.2017 в 11:29