[英]How do I pass the data from my View to the controller?
我有一個禮賓應用程序,用戶將在其中注冊車輛的進出,注冊一個條目我使用兩個表,用戶通過車牌咨詢員工並檢查他是否已注冊,是的我允許他輸入一個他的另一個表,但我找不到將此數據傳遞給 controller 並將其插入數據庫的方法,也就是說,如何從我的View
中獲取數據? 以及如何將數據輸入數據庫? 按照顯示表單的視圖中的代碼和 Controller...
public ActionResult Buscar(string texto)
{
VeiculosDB vb = new VeiculosDB();
var placa = vb.VEICULOGERAL.Where(p => p.PLACA == texto);
return View(placa);
}
在我的 plate 變量中,我有正在顯示的數據,但是我將如何插入這些數據?
@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>
您正在使用帶有 GET 方法的提交表單機制。 這不合邏輯。
GET === retrieve something from server
POST/PUT === send something to server
所以關於你的情況只是改變
using (Html.BeginForm("Buscar", "Portarias", FormMethod.Get))
到
using (Html.BeginForm("Buscar", "Portarias", FormMethod.Post))
由於您正在使用此語句
@model IEnumerable<Portaria.Models.VEICULOGERAL>
您的視圖將根據上述模型的值進行填充。
將 Buscar ActionResult 拆分為[HttpGet]
和[HttpPost]
方法。 使用[HttpGet]
您將從數據中檢索帶有 controller 的數據以供查看。 使用[HttpPost]
操作,您將從視圖檢索數據到數據庫。
首先全局定義你的 db object(在操作方法之外):
VeiculosDB vb = new VeiculosDB();
在您看來,使您的 Form 方法像這樣“發布”:
using (Html.BeginForm("Buscar", "Portarias", FormMethod.Post))
在 beginForm 中,您可以使用帶有提交類型的按鈕,如下所示:
<button type="submit" class="btn btn-primary">Add</button>
它會自動發送數據到[HttpPost]
動作。
您的 [HttpPost] 操作可以是這樣的:
[HttpPost]
public ActionResult Buscar(Vehicle v)
{
vb.Carname.Add(v); //vb from your global db object that I mentioned above.
vb.SaveChanges();
return RedirectToAction("index");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.