I have a concierge application where the user will register an entry and exit of vehicles, to register an entry I work with two tables, the user consults the associate by the license plate and checks if he is registered, yes I allow him to enter one of his another table, but I can’t find a way to pass this data to the controller and insert it into the database, that is, how do I get the data from my View
? And how do I enter the data in the database? follows my code from the View that is displaying the form and the Controller…
public ActionResult Buscar(string texto)
{
VeiculosDB vb = new VeiculosDB();
var placa = vb.VEICULOGERAL.Where(p => p.PLACA == texto);
return View(placa);
}
In a way in my plate variable I have the data that is being displayed, but how would I do an insert with this data ?
@model IEnumerable<Portaria.Models.VEICULOGERAL>
@{
ViewBag.title = "Chegada";
}
<div class="panel-body">
@using (Html.BeginForm("Buscar", "Portarias", FormMethod.Get))
{
<input type="text" class="form-control" name="texto" placeholder="Placa..." />
<div class="form-group">
<div class="">
<input type="submit" value="Buscar" class="btn btn-default" name="texto" style="background-color:aliceblue" />
</div>
</div>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.TIPOVEICULO)
</th>
<th>
@Html.DisplayNameFor(model => model.MARCA)
</th>
<th>
@Html.DisplayNameFor(model => model.ANOMOD)
</th>
<th>
@Html.DisplayNameFor(model => model.ANOFAB)
</th>
<th>
@Html.DisplayNameFor(model => model.VEICULO)
</th>
<th>
@Html.DisplayNameFor(model => model.COR)
</th>
<th>
@Html.DisplayNameFor(model => model.UNIDADE)
</th>
<th>
@Html.DisplayNameFor(model => model.ASSOCIADO)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.CODMARCA)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.CODVEICULO)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.MATRICULA)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.SITUACAO)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.DATAINICIAL)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.DATAFINAL)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.STATUSPRI)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.DESCSTATUSPRI)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.STATUSSEC)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.DESCSTATUSSEC)
</th>
<th hidden>
@Html.DisplayNameFor(model => model.DATAORDEM)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TIPOVEICULO)
</td>
<td>
@Html.DisplayFor(modelItem => item.MARCA)
</td>
<td>
@Html.DisplayFor(modelItem => item.ANOMOD)
</td>
<td>
@Html.DisplayFor(modelItem => item.ANOFAB)
</td>
<td>
@Html.DisplayFor(modelItem => item.VEICULO)
</td>
<td>
@Html.DisplayFor(modelItem => item.COR)
</td>
<td>
@Html.DisplayFor(modelItem => item.UNIDADE)
</td>
<td>
@Html.DisplayFor(modelItem => item.ASSOCIADO)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.CODMARCA)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.CODVEICULO)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.MATRICULA)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.SITUACAO)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.DATAINICIAL)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.DATAFINAL)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.STATUSPRI)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.DESCSTATUSPRI)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.STATUSSEC)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.DESCSTATUSSEC)
</td>
<td hidden>
@Html.DisplayFor(modelItem => item.DATAORDEM)
</td>
</tr>
}
</table>
2
Answers
You are using submit form mechanism with GET method. This isn’t logical.
GET === retrieve something from server
POST/PUT === send something to server
So regarding your case just change
to
Since you’re using this statement
@model IEnumerable<Portaria.Models.VEICULOGERAL>
your View will be populated based on the model’s values above.
Split Buscar ActionResult to
[HttpGet]
and[HttpPost]
methods.With
[HttpGet]
you will retrieve data with controller from data to view.with
[HttpPost]
action, you will retrieve data from view to database.Firstly define your db object globally(outside from action methods):
In your view make your Form method "post" like this:
inside the beginForm you can use a button with submit type like this:
It will automatically send data to
[HttpPost]
action.Your [HttpPost] Action can be like this: