1
I have two tables in my bank: Celular
and Marca
. To register a mobile phone, I need to select a brand for it, ie two models in a view.
Like I do to make two Models are accessed in the same view?
Celularcontroller
public class MarcaCelularViewModel
{
public List<Celular> celulares { get; set; }
public List<Marca> marcas { get; set; }
}
public class CelularController : Controller
{
[HttpGet]
public ActionResult Index()
{
MarcasDAO mDAO = new MarcasDAO();
CelularDAO cDAO = new CelularDAO();
List<Celular> lista_celular = new List<Celular>();
lista_celular = cDAO.getCelular();
List<Marca> lista_marcas = new List<Marca>();
lista_marcas = mDAO.getMarcas();
var model = new MarcaCelularViewModel { celulares = lista_celular, marcas = lista_marcas };
return View(model);
}
View in cell phone registration section
<form method="post">
<div class="form-group">
<label for="nomeCelular">Nome</label>
<input type="text" name="nomeCelular" class="form-control" required />
</div>
<div class="form-group">
<label for="modeloCelular">Modelo</label>
<input type="text" name="modeloCelular" class="form-control" required />
</div>
<div class="form-group">
<label for="idMarca">Marca</label>
<select name="idMarca" class="form-control">
<option selected value="">---</option>
@foreach (var item in Model)
{
...Preenche com as marcas cadastradas no banco
}
</select>
</div>
<button type="submit" class="btn">Adicionar Celular</button>
</form>
View in the part of the table of registered cell phones
<table class="table table-hover" style="background-color:#ffffff; border-radius:10px;">
<thead>
<th>ID</th>
<th>Nome</th>
<th>Modelo</th>
<th>Marca</th>
</thead>
<tbody>
@if (Model != null)
{
foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(Model => item.idCelular)</td>
<td>@Html.DisplayFor(Model => item.nomeCelular)</td>
<td>@Html.DisplayFor(Model => item.modeloCelular)</td>
<td>@Html.DisplayFor(Model => item.idMarca)</td>
</tr>
}
}
</tbody>
</table>
I saw about putting the @model
at the beginning of the index, but I know that this works if it is a model, for several I have no idea how I should declare.