Problems showing parameters in partial view

3

I am consuming a service to consult customer balances, it returns me as an answer an entity with these properties:

public string errorCodigoField { get; set; }
public string errorMensajeField { get; set; }
public decimal saldoRecargasField { get; set; }
public decimal saldoServiciosField { get; set; }

With an input button command to call the corresponding action, using jquery:

function Actualizar() {
        $.ajax({
            url: '/Recargas/ConsultaSaldo',
            cache: false,
            dataType: "html",
            success: function (data) {
                $('#saldosservicios').html(data);
            }
        });

After the call, I have the following condition in the action of the controller, where the return of the partial view is made:

if (saldoInfo.result.errorCodigoField == "00")
{
    ViewBag.SaldoRecargas = Math.Round(Convert.ToDecimal(saldoInfo.result.saldoRecargasField), 2, MidpointRounding.AwayFromZero);

    ViewBag.SaldoServicios = Math.Round(Convert.ToDecimal(saldoInfo.result.saldoServiciosField), 2, MidpointRounding.AwayFromZero);

    return PartialView("ConsultaSaldo", new { _recargas = ViewBag.SaldoRecargas, _servicios = ViewBag.SaldoServicios });

 }
 else
 {
     return RedirectToAction("Index", "Recargas", new { Mensaje = "Ha ocurrido un error en la consulta" });
 }

This would be the partial view:

 @using (Html.BeginForm("ConsultaSaldo", "Recargas", FormMethod.Get, new { id = "formSaldos" }))
{
<div class="saldos">
    <div class="saldosservicios">
        <h4 class="nombresaldo">Saldo Servicios</h4>
        @if (ViewBag.SaldoServicios != null)
        {
            <output class="totalsaldo" >@ViewBag.SaldoServicios</output>                
        }
    </div>

    <div class="saldosrecargas">
        <h4 class="nombresaldo">Saldo Recargas</h4>
        @if (ViewBag.SaldoRecargas != null)
        {
           <output class="totalsaldo">@ViewBag.SaldoRecargas</output>
        }           
    </div>
    <div>
        <input class="btnConsultar" type="button" value="Consultar" onclick="Actualizar()"/>
    </div>
 </div>
}

And the call to the partial view from the main view I do it this way:

<div id="saldos-section">
    @Html.Partial("ConsultaSaldo", new { SaldoRecargas = ViewBag.SaldoRecargas, SaldoServicios = ViewBag.SaldoServicios })
</div>

I have noticed in the debugging that the ViewBag does contain the expected values:

But for some reason the results are not shown on the screen. If you could give me some kind of guidance, I will be very grateful.

Greetings.

    
asked by Jesus Mejia 16.08.2017 в 17:33
source

5 answers

0

Thanks to all who made their contributions. I have found the solution by other means so I would like to share the following:

First I changed the result type of the query action from Task<ActionResul> to Task<JsonResult> .

The conditional of Action of Controller looks like this:

if (saldoInfo.result.errorCodigoField == "00")
{
     var SaldoRecargas = Math.Round(Convert.ToDecimal(saldoInfo.result.saldoRecargasField), 2, MidpointRounding.AwayFromZero);

     var SaldoServicios = Math.Round(Convert.ToDecimal(saldoInfo.result.saldoServiciosField), 2, MidpointRounding.AwayFromZero);

     return Json(new string[] { saldoInfo.result.errorCodigoField, saldoInfo.result.errorMensajeField, SaldoRecargas.ToString(), SaldoServicios.ToString() }, JsonRequestBehavior.AllowGet);
}
else
{
     return Json(new string[]{ saldoInfo.result.errorCodigoField, saldoInfo.result.errorMensajeField, "",""}, JsonRequestBehavior.AllowGet);
}

The partial view was restructured, remaining as follows:

@using (Html.BeginForm("ConsultaSaldo", "Recargas", FormMethod.Get, new { id = "formSaldos" }))
{
<div class="saldos">
    <div class="saldosservicios">
        <h4 class="nombresaldo">Saldo Servicios</h4>
            <output class="totalsaldo" id="outsaldoservicio" ></output>                
    </div>

    <div class="saldosrecargas">
        <h4 class="nombresaldo">Saldo Recargas</h4>
           <output class="totalsaldo" id="outsaldorecargas"></output> 
    </div>
    <div>
        <input class="btnConsultar" type="button" value="Consultar" onclick="Actualizar()"/>
    </div>
    </div>
}

The balance update script looks like this:

<script type="text/javascript">
    function Actualizar() {
        $.ajax({
            url: '/Recargas/ConsultaSaldo',
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $("#outsaldoservicio").html(data[3])
                $("#outsaldorecargas").html(data[2])
            }
        });
    }
</script>

Finally the partial view call, in the main view was like this:

<div id="saldos-section">
            @Html.Partial("ConsultaSaldo", new { SaldoRecargas = ViewBag.SaldoRecargas, SaldoServicios = ViewBag.SaldoServicios })                
</div>

Final result:

    
answered by 16.08.2017 / 23:50
source
0

Probably because of the output element. Outpot in HTML is used to display results of operations. In this case, you are loading a data from the controller, Try printing on a label element and add @ Html.Raw (ViewBag.Mydata) .

<label class="totalsaldo">@Html.Raw(ViewBag.SaldoRecargas)</label>
    
answered by 16.08.2017 в 17:43
0

I recommend you try removing the ViewBag:

Model

public class Saldos
{
    public int SaldoRecargas  { get; set; }
    public int SaldoServicios { get; set; }
}

Controller

if (saldoInfo.result.errorCodigoField == "00")
{
    var datos = new Saldos();
    datos.SaldoRecargas = Math.Round(Convert.ToDecimal(saldoInfo.result.saldoRecargasField), 2, MidpointRounding.AwayFromZero);

    datos.SaldoServicios = Math.Round(Convert.ToDecimal(saldoInfo.result.saldoServiciosField), 2, MidpointRounding.AwayFromZero);

    return PartialView("ConsultaSaldo", datos);

}
else
{
    return RedirectToAction("Index", "Recargas", new { Mensaje = "Ha ocurrido un error en la consulta" });
}

Partial View

@model MiNamespace.Saldos

@using (Html.BeginForm("ConsultaSaldo", "Recargas", FormMethod.Get))
{
<div class="saldos">
    <div class="saldosservicios">
        <h4 class="nombresaldo">Saldo Servicios</h4>
        @if (@Model.SaldoServicios != null)
        {
            @Model.SaldoServicios
        }
    </div>

    <div class="saldosrecargas">
        <h4 class="nombresaldo">Saldo Recargas</h4>
        @if (@Model.SaldoRecargas != null)
        {
            @Model.SaldoRecargas
        }           
    </div>
    <div>
        <input class="btnConsultar" type="button" value="Consultar" onclick="Actualizar()"/>
    </div>
</div>
}

Call to partial view:

<div id="saldos-section">
    @Html.Partial("ConsultaSaldo")
</div>

I would also like to see the code of the action complete, including the signature of the method.

    
answered by 16.08.2017 в 18:32
0

When rendering the partial view, the object that you send as an argument is a Model :

<div id="saldos-section">
    @Html.Partial("ConsultaSaldo",
   /* Modelo -> */ new { SaldoRecargas = ViewBag.SaldoRecargas, SaldoServicios = ViewBag.SaldoServicios })
</div>

Therefore, try modifying your partial view in the following way:

@model dynamic
@using (Html.BeginForm("ConsultaSaldo", "Recargas", FormMethod.Get, new { id = "formSaldos" }))
{
<div class="saldos">
    <div class="saldosservicios">
        <h4 class="nombresaldo">Saldo Servicios</h4>
        @if (Model.SaldoServicios != null)
        {
            <output class="totalsaldo" >@Model.SaldoServicios</output>                

        }
    </div>

    <div class="saldosrecargas">
        <h4 class="nombresaldo">Saldo Recargas</h4>
        @if (Model.SaldoRecargas != null)
        {
           <output class="totalsaldo">@Model.SaldoRecargas</output>
        }           
    </div>
  <div>
    <input class="btnConsultar" type="button" value="Consultar" onclick="Actualizar()"/>
  </div>
</div>
}

The best thing is that you create a model class with the properties that you are going through:
namespace TuSitio.Models
{
    public class ModeloDeVistaParcial
    {
        public decimal SaldoRecargas { get; set; }
        public decimal SaldoServicios { get; set; }
    }
}

You pass it as the model in partial view:

@Html.Partial("ConsultaSaldo",
              new TuSitio.Models.ModeloDeVistaParcial()
                  {
                      SaldoRecargas = ViewBag.SaldoRecargas,
                      SaldoServicios = ViewBag.SaldoServicios
                  })

And you declare it as the model in that view:

@model TuSitio.Models.ModeloDeVistaParcial
    
answered by 16.08.2017 в 18:48
0

Html.Partial and Html.RenderPartial do not execute the cycles of a controller as a normal request does, which means that the methods of the controller are not executed, but they parsean the html of the views directly and therefore the values of ViewBag never end up being assigned.

Use Action.RenderAction(viewname, requestParameters) that executes the normal cycles of a Request and you will be shown the ViewBag s:

<div id="saldos-section">
@{  
   Html.RenderAction("ConsultaSaldo", new { SaldoRecargas = ViewBag.SaldoRecargas, SaldoServicios = ViewBag.SaldoServicios });
}
</div>
    
answered by 16.08.2017 в 21:01