I need to Refresh a View When using onchage in a select. in MVC 4 razor C #

2

I have a problem with the use of a

select class="selection-box large-select" name="drop" id="drop"onchange="rut()">

which I am using it to send its value to a

[HttpPost]        
public ActionResult MiPerfil(string pRutIn) 
{
    pFechaIn = "15/06/2014"; 
    var tipo1 = clsMW_SPB_IMF_PFL_CLT.execute(pRutIn, pFechaIn);
    return View(tipo1);            
}

so I can load a table:

After it loads, the combobox allows me to select another user and this table should refresh or the page with the new user's data. I've been trying to work with AJAX .

JavaScript

function rut() {

    debugger;
    var pRutIn = $("#drop").val();
    $.ajax({
        url: '~/User/MiPerfil',
        type: 'POST',
        datatyoe: 'JSON',
        data: ({ pRutIn: +pRutIn }),
        success: function (result) {
            $("#drop").html("");
            $.each($.result), function (i, rut) {
                $("#drop").append($('<option></option>').val(drop.ondrop).html(""))
            }

        },
        error: function () {
            alert("error en la respuesta")
        }
    });
}

How can I refresh the data every time I select a user in the combobox and also how to refresh the page?

Update

You still do not make any changes when selecting a select option, the code is like this:

function SelectionChanged() {

    debugger;
    var pRutIn = $("#drop").val();
    $.ajax({
        url: '~/User/MiPerfil',
        type: 'POST',
        dataType: 'JSON',
        data: ({ "pRutIn": pRutIn }),
        success: function (result) {
            $("#drop").html("");
            $.each($.result), function (i, rut)
            {
                $("#drop").append($('<option></option>').val(drop.ondrop).html("")) **esta parte no se si esta bien...**
                }

        },
        error: function () {
            alert("error en la respuesta")
        }
    });
}
    
asked by Manu Keteimporta 16.09.2016 в 19:43
source

1 answer

1

Greetings Manu Keteimporta (funny nick), from what I see, it's about updating only that part of the page. To achieve this instead of rendering the whole view, do it with a partial view:

return PartialView(tipo1)

on the client side, it replaces what you have in data with this:

data: { "pRutIn": pRutIn },

is dataType no datatyoe .

Update

and to call the function constantly in each on change of select :

$('#drop').on('change', function() {
   rut();
})

Update 2

This part:

$.each($.result), function (i, rut)
                    {
                        $("#drop").append($('<option></option>').val(drop.ondrop).html(""))
                    }

Replace it with:

$.each(result, function (i, rut) {
    $('#drop').append($('<option>', { 
        value: rut.value,
        text : rut.text 
    }));
});
    
answered by 16.09.2016 / 19:53
source