Query in . NET Core

Asked

Viewed 308 times

1

I am learning the logic of MVC applied in . NET Core but I have some doubts about doing the process CRUD, more specifically the Consultation.

  • How can I make the button click communicate with a Controller request information for the Model, the same return the result to the Controller and the results are displayed on the screen?

Follows code.

View.

@model WebApplication1.Models.CepViewModel
<h2>Manutenção de Cep</h2>
<div class="container" style="margin-top: 50px;">
<form asp-controller="Cep" asp-action="Cadastro" method="post">
    <div class="form-group">
        <label class="control-label">CEP</label>
        <input asp-for="CepCodigo" style="margin-left: 90px" width="50px" />
        <label style="margin-left: 90px" class="control-label">LOGRADOURO</label>
        <input asp-for="CepLogr" style="margin-left: 90px" width="50px" />
        <br />
        <label class="control-label">ENDEREÇO</label>
        <input asp-for="CepEnd" style="margin-left: 40px" width="50px" />
        <label style="margin-left: 90px" class="control-label">COMPLEMENTO</label>
        <input asp-for="CepCompl" style="margin-left: 82px" width="50px" />
        <br />
        <label class="control-label">BAIRRO</label>
        <input asp-for="CepBairro" style="margin-left: 64px" width="50px" />
        <label style="margin-left: 90px" class="control-label">CIDADE</label>
        <input asp-for="CepCidade" style="margin-left: 138px" width="50px" />
        <br />
        <label>UF</label>
        <input asp-for="CepUF" style="margin-left: 100px; width: 50px;" />
        <br />
        <br />
        <div class="form-group">
            <input type="submit" value="Consulta" />
        </div>
    </div>
    </div>
</form>

Controller.

   public class CepController : Controller
    {
        public IActionResult Cadastro()
        {
            return View();
        }
        public IActionResult Consulta()
        {
            return View();

        }
    }
}

Model.

    public class CepViewModel
    {
        public string CepCodigo { get; set; }
        public string CepLogr { get; set; }
        public string CepEnd { get; set; }
        public string CepCompl { get; set; }
        public string CepBairro { get; set; }
        public string CepCidade { get; set; }
        public string CepUF { get; set; }
        public int CepSetor { get; set; }
        public string CepRegiao { get; set; }
        public string CepRota { get; set; }


        public CepViewModel TakeCepById(int Id)
        {
            CepViewModel cep = null;

            using (var connection = new SqlConnection("DBConect"))
            {

                cep = connection.Query<CepViewModel>("Select * From CEP WHERE CepCodigo = @CepCodigo",
                        new { CepCodigo = Id }).SingleOrDefault();
                return cep;
            }

        }
    }
}
  • 1

    I think you already solved that one, no?

  • @Leandroangelo yes friend, thank you! But if you want to share how you apply your logic to this example, you are also welcome

  • Ideal would be for you to put your solution and mark as answered, so that the question does not remain open without answer or remove it, but the first option is that it brings more benefit to other users..

1 answer

0

In the form you direct to the controller with your View action:

<form asp-controller="Cep" asp-action="Consulta">

In the controller you indicate that you are sending by POST

[HttpPost]
        public IActionResult Consulta(int Id)
        {

            return View();
        }

Browser other questions tagged

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