I'm learning the MVC logic applied in .NET Core but I have some doubts about doing CRUD process, more specifically Query .
- How can I make the click of the button communicate with a Controller and the same, request the information for the Model , it returns the result for Controller and the results are displayed on the screen?
Follow the 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;
}
}
}
}