request data from the asp.net mvc c # driver

0

I'm starting a development on asp.net MVC C #.

What I need to know is how I can, from the controller, call a subroutine of the model. The idea is to pass as a parameter a key, and that it recovers the corresponding description that is in a database.

The query to the database is made with sqlcommand .

I already created a public class ( public class buscadato ) but it does not do anything, I do not know what I'm doing wrong or what I'm missing.

Can you provide me with an example of how to define the class and how to call it from the controller to pass and return a string?

IN THE MODEL:     public class search Country     {         public string WCVEPAI;         public string WDESPAI;

    public buscarPais(string WDATO1, string WDATO2)
    {
        WCVEPAI = WDATO1;
        WDESPAI = WDATO2;
    }

    public string xbuscarPais()
    {
        WDESPAI = "x";
        string CONBD = ConfigurationManager.ConnectionStrings["BDPROADIN"].ConnectionString;
        var BDCON = new SqlConnection(CONBD);
        BDCON.Open();

        var INSSQL = "SELECT * FROM COAPI WHERE CVEPAI = " + WCVEPAI;
        var CMDSQL = new SqlCommand(INSSQL, BDCON);
        SqlDataReader reader = CMDSQL.ExecuteReader();

        if (reader.Read())
        {
            WDESPAI = (string)reader["DesPai"];
        }
        else
        {
            WDESPAI = "No existe en catalogo.";
        }
        System.Console.WriteLine(WDESPAI);
        reader.Close();
        BDCON.Close();
        WDESPAI = "AB";
        return WDESPAI;
    }
}
}

IN THE CONTROLLER:

                    CatPai BP = new CatPai();
                    buscarPais bp = new buscarPais(WCVEPAI,WDESPAI);
    
asked by JCRH 02.12.2016 в 01:22
source

2 answers

0

There are multiple ways you can make the call to that function. I imagine that what you want is to show that value in the view, so you could create a model-view:

public class BuscaPaisVM {
    public string Nombre {get;set;}
    //Otras propiedades que desees mostrar
}

And it would be called something like this:

public ActionResult BuscarPais(string wdato1){
    string wdato1 = string.Empty;
    var bp = new buscarPais(WCVEPAI,WDESPAI);
    var _out = new BuscaPaisVM{ Nombre = bp.xbuscarPais(), /* Otras propiedades */ };
    return View(_out);
}

Another possibility is that you want to call the method using Ajax. That is, once the page is loaded. For that you do not need a model-view and you can do this:

public ActionResult BuscarPais(string wdato1){
    var bp = new buscarPais(wdato1, string.Empty);
    return Json(bp.xbuscarPais());
}

And, as I said, you would call it using an Ajax method in the HTML of your view.

Now, there are some problems with your class look for Country:

  • Members WCVEPAI and WDESPAI should be properties
  • The WDESPAI member seems to me that he should not be a member of the class since we do not do anything with that member but assign it, but the xbuscarPais method returns it as an exit. This can be confusing. It would be better to simply return the value in the function xbuscarPais or, better yet, create it as property and call xbuscarPais in your Set method.
  • At the end of xbuscarPais you set WDESPAI to a constant value ("AB") therefore you undo everything you did previously.
  • In xbuscarPais you perform a select * from. However, you only need one field in the database: DesPai. It is important to always get used to include all fields in a database query even if the code is short. The use of select * from should be limited to the query editor only.

Here is an improved version of the Search Country class:

public static class BuscarPais { 
    public static string Buscar(string wvcepai)
    {
        string _out = "x";
        string CONBD = ConfigurationManager.ConnectionStrings["BDPROADIN"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(CONBD))
        {
            conn.Open();

            var INSSQL = "SELECT DesPai FROM COAPI WHERE CVEPAI = @cvepai";
            var CMDSQL = new SqlCommand(INSSQL, conn);
            CMDSQL.Parameters.Add(new SqlParameter("cvepai", cvepai));
            using (SqlDataReader reader = CMDSQL.ExecuteReader())
            {
                if (reader.Read())
                {
                    _out = (string)reader["DesPai"];
                }
                else
                {
                    _out = "No existe en catalogo.";
                }
                System.Console.WriteLine(_out);
                reader.Close();
            }
            conn.Close();
        }

        return _out;
    }
}

To call it, just the following is enough:

string despais = BuscarPais.Buscar(WCVEPAI);
    
answered by 02.12.2016 / 12:09
source
0

Friend if you are using MVC C # you can instantiate a model of your database in the project and you can make calls and queries with LINQ without using that type of commands.

I recommend you read a bit the documentation of the VISTA CONTROLLER MODEL so you can create your model and all the queries will be much easier.

    
answered by 28.12.2016 в 20:27