2
I’m a beginner in ASP.NET MVC and I need to create a Dropdown in a vehicle registration form where I can choose only one driver related to the vehicle that is previously registered in the database (created via Entity Framework). My code is currently like this:
Conductor
public class Condutor
{
public int CondutorId { get; set; }
[Required]
public int Matricula { get; set; }
[Required]
public string PrimeiroNome { get; set; }
[Required]
public string UltimoNome { get; set; }
[Required]
public string CPF { get; set; }
}
Vehicle
public class Veiculo
{
public int Id { get; set; }
[Required]
public string Modelo { get; set; }
[Required]
public string Marca { get; set; }
[Required]
public string Tipo { get; set; }
}
Controller
[AutorizacaoFilter] // filtro de autorizacao, localizado na pasta "Filters"
public class VeiculoController : Controller
{
// atributos do controller para acesso ao banco de dados
private VeiculoDAO dao;
// construtor da classe
public VeiculoController(VeiculoDAO dao)
{
this.dao = dao;
}
public IActionResult Index()
{
IList<Veiculo> lista = dao.Lista();
return View(lista);
}
[HttpPost]
public IActionResult IncluirVeiculo(Veiculo veiculo)
{
dao.IncluirVeiculo(veiculo);
return RedirectToAction("Index");
}
public IActionResult Cadastro()
{
var model = new Veiculo();
return View(model);
}
// remove um veículo, através do seu id cadastrado na tabela do banco de dados
public IActionResult RemoverVeiculo(int id)
{
dao.RemoverVeiculo(id);
return RedirectToAction("Index");
}
// localiza um veículo pelo seu Id e o exibe na View "Ver Cadastro", para ser editado
public IActionResult VerCadastro(int id)
{
Veiculo veiculo = dao.BuscaVeiculoPorId(id);
return View(veiculo);
}
// edita o registro do veículo e reflete as alterações no BD
[HttpPost]
public IActionResult EditarVeiculo(Veiculo veiculo)
{
dao.AtualizaCadastro(veiculo);
return RedirectToAction("Index");
}
public IActionResult BuscaVeiculo(string termo)
{
IList<Veiculo> veiculos = dao.BuscaPeloTermo(termo);
return View("Index", veiculos);
}
}
View
Ignore Enum, it was just for testing, while I couldn’t solve Dropdown
@model Veiculo
<div class="container">
<br />
<h2>Cadastrar veículo</h2>
<br />
<form class="form-horizontal" action="/Veiculo/IncluirVeiculo" method="post">
<div class="form-group">
<label class="control-label col-sm-2" for="text">Modelo:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="modelo" style="max-width:50%" placeholder="Ex: Palio, Gol, Corsa, etc" name="modelo" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="text">Marca:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="marca" style="max-width:50%" placeholder="Ex: Chevrolet, Honda, Nissan, etc" name="marca" />
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="text">Tipo:</label>
<div class="col-sm-10" style="max-width:43%">
@Html.DropDownListFor(v => v.Tipo,
new SelectList(Enum.GetValues(typeof(Tipo))),
"-- Escolha uma opção --",
new { @class = "form-control"})
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Salvar</button>
<button type="reset" class="btn btn-info">Limpar dados</button>
</div>
</div>
</form>
</div>
Solving this example, the idea is to use other dropdowns as well, among different models. The current difficulty of mine was that, as the View is strongly typed in Vehicle, I could not create this "link" with the Driver class.
In this case, how could I create this type of Dropdown, via Entity Framework using LINQ and LAMBDA?
Vehicle and driver relate?
– Augusto Henrique
There is no relationship between them
– Augusto Albano
You need to record
CondutorId
on the tableVeiculo
?– Raquel Pinheiro