Assign value to a returned input from a controller

2

I have this script in my view

    $(document).ready(function () {
        $("#PlanVenta").change(function () {
            $("#Precio").empty();
            $.ajax({
                type: 'POST',
                url:'@Url.Action("PreciosPorTipoVenta")',
                datatype:'json',
                data:{Id_Plan:$("#PlanVenta").val(),Id_Articulo:$("#Id_Articulo").val()},
                success:function(precios){
                    $("#Precio").val(?????);
                },
            });
        });
    });
which sends data to this controller

public JsonResult PreciosPorTipoVenta(int Id_Plan, int Id_Articulo)
        {
            return Json(querty, JsonRequestBehavior.AllowGet);
        }

and I have this input to which I want to assign the value that the controller returns to me from ajax

<input type="text" id="Precio" name="Precio" />

what should I put there because I already try to put precios.Value as I had seen in an example but nothing comes out

    
asked by Xique 31.08.2016 в 20:44
source

2 answers

2

The answer of oscar should be valid since you receive a json in this way {precios:[{precio:600}]} .

Try $('#Precio').val(precios[0].precio); since it is possible that you are getting an array with more data

    
answered by 31.08.2016 / 21:43
source
3

You need to go through the returned json, in this case the handles as price, you have to go through the json price depending on the attributes that it has and that you want to show in the example example:

$(document).ready(function () {
        $("#PlanVenta").change(function () {
            $("#Precio").empty();
            $.ajax({
                type: 'POST',
                url:'@Url.Action("PreciosPorTipoVenta")',
                datatype:'json',
                data:{Id_Plan:$("#PlanVenta").val(),Id_Articulo:$("#Id_Articulo").val()},
                success:function(precios){
                    //precios es la respueta de tu contralador
                    $('#Precio').val(precios.propiedad);

                },
            });
        });
    });
    
answered by 31.08.2016 в 20:53