getters and setters do not work with Httpget

Asked

Viewed 43 times

0

Good afternoon guys, I have the following code:

    [HttpGet]
    public JsonResult ObterGridParceirosComPaginacao(PaginacaoVueTableViewModel filtro)
    {
        Resultado resultado = new Resultado();
        var viewModel = new PaginacaoVueTableRetornoViewModel();

        return Json(viewModel);
    }

With the following Viewmodel:

public class PaginacaoVueTableViewModel
{

    private int page;
    private string orderBy;

    public int Page
    {
        get
        {
            return page;
        }
        set
        {
            page = value > 0 ? value - 1 : value;
        }
    }
    public string OrderBy
    {
        get
        {
            return orderBy;
        }
        set
        {
            orderBy = value == null ? string.Empty : value;
        }
    }
}

The body of the method Obtaindparceiroscompagination is not yet implemented, but my problem is when I receive Viewmodel as a parameter, the setters are not applying the rules that are set in them. I put some breakpoints and realized that at no time the getters and setters are called, so they don’t apply the rules. Does anyone know what it could be?

Note: The Viewmodel comes with all the correct values, sent from Javascript, I just need these values to pass the validations of getters and setters.

  • setters make no change without being called

  • But isn’t there some way I can call them? Or is there some better way to do what I’m trying to do?

  • The problem is that the parameter filtros null arrives? If so, the problem should be mapping from JSON to C#class. In ASP.Net Core, using json {
 "page":1,
 "orderBy":"teste"
}worked because the mapping rule is Camelcase for Pascalcase. If you want a JSON other than this one, you can use Jsonproperty to explicitly tell . Net what the Json name of the property is.

  • to call the Setter in your case just put the name of the property, and then the moment it sets it will make its logic there ex: filter.Orderby = null; //when you do this here it will fill in with ""

  • So the filters parameter, it comes filled, but it does not apply the rules that are in the setters, for example, if I send from Javascript the attribute Page with the value 10, according to the rule it should turn 9, but it is maintained as 10, ie the rule was not executed. And at no time does he stop at the breakpoint I put on Setter.

1 answer

0

No problem at all with your setters, the error may be in the way you are making the request, see the demo below.

However, note that in assigning the variable teste where the PaginacaoVueTableRetornoViewModel instantiated, but no value is assigned to one of the properties... for reasons, logic set is not invoked...

And that would be the behavior presented in his action ObterGridParceirosComPaginacao that receives this model under the parameter filtro, you do not use it and then below send to View an object "empty".

var viewModel = new PaginacaoVueTableRetornoViewModel();
return Json(viewModel);

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.