Pass selected value from DropDownList to modal window asp mvc

0

I need to pass the selected value of a DropDownList loaded by means of a SelectList to a modal window, for that I am using this line.

         

    @Html.LabelFor(model => model.CompañiaId, htmlAttributes: new { @class = "control-label col-sm-1" })
    <div class="col-sm-4">
        @Html.DropDownListFor(x => x.CompañiaId, Model.CompañiasViewModels, new { @class = "form-control", @id = "dropDownList" })
        @Html.ValidationMessageFor(model => model.CompañiasViewModels, "", new { @class = "label label-danger" })
    </div>
    <div>
        <a class="btn btn-warning disabled" data-modal="" data-target="#myModal" data-to data-toggle="modal" href='@Url.Action("CreatePopUp", "Usuario", new {id = 5})' id="btnCreateUser" "><span class="glyphicon glyphicon-plus"></span></a>
    </div>

</div>

As you will see the value of the Id is hard-coded and I need it to be dynamic, this is the action of the controller where I need the selected id to arrive.

[HttpGet]
public ActionResult CreatePopUp(int id)
{
   UsuarioViewModel usuarioViewModel = new UsuarioViewModel();
   usuarioViewModel.RolesViewModels = GetRoles();
   usuarioViewModel.CompañiaId = Convert.ToInt32(id);
   return View("CreatePopUp", usuarioViewModel);
}

To show the modal windows I use the following javascript:

$(function () {
    $.ajaxSetup({ cache: false });
    $("a[data-modal]").on("click", function (e) {

        $('#myModalContent').load(this.href, function () {
            $('#myModal').modal({
                /*backdrop: 'static',*/
                keyboard: true
            }, 'show');
            bindForm(this);
        });
        return false;
    });
});

function bindForm(dialog) {
    $('form', dialog).submit(function () {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {
                if (result.success) {
                    $('#myModal').modal('hide');
                    $('#replacetarget').load(result.url);
                } else {
                    $('#myModalContent').html(result);
                    bindForm(dialog);
                }
            }
        });
        return false;
    });
}

This function is used by other buttons, so taking the value directly here would not help me,

How can I send the value of the DropDownlist from this line?

 <a class="btn btn-warning disabled" data-modal="" data-target="#myModal" data-to data-toggle="modal" href='@Url.Action("CreatePopUp", "Usuario", new {id = 5})' id="btnCreateUser" title="Nuevo Usuario de Compañía "><span class="glyphicon glyphicon-plus"></span></a>
    
asked by jose luis garcia 01.04.2016 в 06:56
source

2 answers

1

Remember that for model binding to work, you must match the name of the html control with the action parameter.

There are several ways to achieve this, you could send the value from javascrit on time using

@Html.DropDownListFor(x => x.CompañiaId, Model.CompañiasViewModels, new { @class = "form-control"})

$('form', dialog).submit(function () {

    var params={
        Id : $("#CompañiaId").val()
    };

    $.ajax({
        url: this.action,
        type: this.method,
        data: params,
        success: function (result) {
            if (result.success) {
                $('#myModal').modal('hide');
                $('#replacetarget').load(result.url);
            } else {
                $('#myModalContent').html(result);
                bindForm(dialog);
            }
        }
    });

    return false;

});

You will see that I do not define any id in the Html.DropDownListFor and I take the value from the submit in jquery to send it punctually as a parameter.

That's one way, now if you want to serialize the form you could simply define a name in the DropDownList that matches the parameter

@Html.DropDownListFor(x => x.CompañiaId, Model.CompañiasViewModels, new { @class = "form-control", Name = "companiaId" })

[HttpGet]
public ActionResult CreatePopUp(int companiaId)
{
   UsuarioViewModel usuarioViewModel = new UsuarioViewModel();
   usuarioViewModel.RolesViewModels = GetRoles();
   usuarioViewModel.CompañiaId = Convert.ToInt32(id);
   return View("CreatePopUp", usuarioViewModel);
}

As you will see in the dropdownlist I defined the attribute name and I match it with the name of the parameter, so when serializing the binding, it will map the values.

    
answered by 01.04.2016 в 10:49
0

The first thing you suggest is not right for me since I need to have the Id from DropDownList to refer to it in another script which load a table depending on the section of this.

I tried defining a Name="id" and defined the same name in the controller but I keep getting errors:

The parameter dictionary contains a NULL entry for the 'id' parameter of the type that does not accept NULL values' System.Int32 'from the' System.Web.Mvc.ActionResult CreatePopUp (Int32) 'method in' CntrlLine.WebUi. Controllers.UsuarioController '. An optional parameter must be a reference type, a type that accepts NULL values or must be declared as an optional parameter. Parameter name: parameters

@Html.DropDownListFor(x => x.CompañiaId, Model.CompañiasViewModels, new { @class = "form-control", @id = "dropDownList", @Name = "id" })

Controller:

        public ActionResult CreatePopUp(int id)
    {

        UsuarioViewModel usuarioViewModel = new UsuarioViewModel();
        usuarioViewModel.RolesViewModels = GetRoles();
        usuarioViewModel.CompañiaId = Convert.ToInt32(id);
        return View("CreatePopUp", usuarioViewModel);
    }

Script:

$(function () {
$.ajaxSetup({ cache: false });
$("a[data-modal]").on("click", function (e) {

    $('#myModalContent').load(this.href, function () {
        $('#myModal').modal({
            /*backdrop: 'static',*/
            keyboard: true
        }, 'show');
        bindForm(this);
    });
    return false;
});
});

function bindForm(dialog) {
$('form', dialog).submit(function () {
    $.ajax({
        url: this.action,
        type: this.method,
        data: $(this).serialize(),
        success: function (result) {
            if (result.success) {
                $('#myModal').modal('hide');
                $('#replacetarget').load(result.url);
            } else {
                $('#myModalContent').html(result);
                bindForm(dialog);
            }
        }
    });
    return false;
});
}
    
answered by 02.04.2016 в 18:48