Help with the search method c # mvc razor

1

That such a friend I turn to you again to solve a problem that I think is simple but I could not find the solution.

1- I am using the sew component to be able to enter various data into a search form that contains 2 dropdownlist.

2.- the 2 dropdownlists may be full or, if applicable, only one of the 2

3.- The model I use is the following

 [NotMapped]
public class SearchProcedimiento:MtoProcedimiento
{
    public int? Page { get; set; }

    public IPagedList<MtoProcedimiento> result_MtoProcedimientos { get; set; }


    public IEnumerable<MtoProcedimiento> ListProcedimientos { get; set; }

    public int[] itemLicitaciones { get; set; }

    public IEnumerable<MtoTipoEvento> ListTipoEventos { get; set; }

    public int[] itemTipoEventos { get; set; }


}
  • The problem begins when I ask the controller in the following way.

    var list = from t in db.MtoProcedures
                    where (model.itemLicitaciones.Contains ((int) t.MtoProcedimientoId) ||                        model.itemTypeEvents.Contains ((int) t.MtoTipoEventoId))
                    select t;

  • Well it turns out that when the 2 dropdownlists are full it makes the query, but when one of the 2 is empty it sends me the following error.

    No se puede crear un valor de constante NULL de tipo 'System.Int32[]'. Solo se admiten tipos de entidad, tipos de enumeración o tipos primitivos en este contexto.
    

    I think it's because, the array is null, as it could validate to be able in case the array is empty put in the 0 position of the array the value "0" so that it considers all the records in my query.

    or you could give me a help on how to generate this query.

    Thank you very much greetings.

        
    asked by Horacio Xochitemol 05.06.2018 в 20:43
    source

    2 answers

    1

    If I did not misinterpret your problem, it would be enough for you to validate that mtoProcedimientoId or MtoTipoEventoId are null, I would try the following, sorry if there is a syntax error, that I do not have IDE at hand

    var list = from t in db.MtoProcedimientos
    where 
    ((t.MtoProcedimientoId == null || model.itemLicitaciones.Contains((int)t.MtoProcedimientoId))
     || 
    (t.MtoTipoEventoId == null || model.itemTipoEventos.Contains((int)t.MtoTipoEventoId)))
    select t;
    

    That yes MtoProcedimientoId and MtoTipoEventoId must be of some type <Nullable>

    I hope it's what you were looking for! but correct yourself in the comments and we keep looking for the return!

    Greetings and successes!

        
    answered by 05.06.2018 в 21:48
    0

    I do not know if it's the best solution but what I did was change the query code to the following.

      var list = from t in db.MtoProcedimientos
                       where model.itemLicitaciones.Contains(t.MtoProcedimientoId) ||
                             model.itemTipoEventos.Contains(t.MtoTipoEventoId)  
                       select t;
    

    now in the script from where I call it, it's like this.

    $('#NewSearchResumen').submit(function (event) {
    
        event.preventDefault();
    
        var itemlicitaciones = $('#itemLicitaciones').val();
        var itemtipoeventosventos = $('#itemTipoEventos').val();
    
    
        if (itemlicitaciones != null || itemtipoeventosventos != null) {
    
            var NewModel = {
                ItemLicitaciones: itemlicitaciones,
                itemTipoEventos: itemtipoeventosventos
            };
    
            var booksResumen = $("#booksResumen");
    
            $.ajax({
    
                type: 'POST',
                url: _urlBase + "Procedimientos/ResumenProcedimientos",
                contenType: "application/html; charset=utf-8",
                data: { model: NewModel, "__RequestVerificationToken": $('input[name=__RequestVerificationToken]').val() },
                datatype: 'json',
                beforeSend: function (objeto) {
                    openLoad();
                },
                success: function (data) {
                    closeLoad();                    
                    booksResumen.html('');
                    booksResumen.html(data);
                },
                error: function (xhr, status, error) {
                    closeLoad();
                    alert(xhr.responseText);
                    console.log(xhr.status);
                    console.log(status);
                }
            });
    
    
        }
        else {
            sweetAlert("Oops...", 'Seleccione criterios de busqueda validos', "error");
        }
    });
    

    I have already done tests and he sends me the partial view; however, the console shows me the following error.

    TypeError: $(...).valid is not a function[Saber más] bootstrap.validate.js:28:13
    

    onFormSubmit link dispatch link add / elemData.handle link

        
    answered by 08.06.2018 в 20:09