[英]Change a specific column in table in ASP.NET MVC
我是ASP.NET MVC的新手。 試圖從表中的記錄列表中獲取一條記錄。 在每個記錄中,我都有一個設置為1的字段status
。在這里,我僅在View
和Button
顯示一個記錄。 當我單擊一個按鈕時,必須將數據庫中該記錄的狀態更改為3,然后在同一視圖中獲取另一個記錄。 目前,我已經通過選擇腳手架模板列表創建了此視圖。
控制器:
public class HomeController : Controller
{
dbSampleEntities db = new dbSampleEntities ();
//
// GET: /Home/
public ActionResult Index()
{
var result = db.Visits.Where(x=> x.Status == "1").OrderBy(r => Guid.NewGuid()).Take(1);
return View(result);
}
[HttpPost]
public ActionResult ChangeStatus(int VisitorID)
{
return RedirectToAction("Index");
}
}
Index.html:
@model IEnumerable<SampleApp.Visit>
@{
ViewBag.Title = "Index";
}
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.VisitorID)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<th>
@Html.DisplayNameFor(model => model.LastName)
</th>
<th>
@Html.DisplayNameFor(model => model.Phone)
</th>
<th>
@Html.DisplayNameFor(model => model.VisitTypeID)
</th>
<th>
@Html.DisplayNameFor(model => model.BranchID)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
<th>
@Html.DisplayNameFor(model => model.EmailID)
</th>
<th>
@Html.DisplayNameFor(model => model.Comments)
</th>
<th>
@Html.DisplayNameFor(model => model.UserCreated)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedTime)
</th>
<th>
@Html.DisplayNameFor(model => model.UpdatedTime)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.VisitorID)
</td>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Phone)
</td>
<td>
@Html.DisplayFor(modelItem => item.VisitTypeID)
</td>
<td>
@Html.DisplayFor(modelItem => item.BranchID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Status)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailID)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments)
</td>
<td>
@Html.DisplayFor(modelItem => item.UserCreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedTime)
</td>
<td>
@Html.DisplayFor(modelItem => item.UpdatedTime)
</td>
<td>
@using (Html.BeginForm("ChangeStatus", "Home", FormMethod.Post))
{
@Html.HiddenFor(modelItem => item.VisitorID)
<input type="submit" value="Change Status" />
}
</td>
</tr>
}
</table>
在這里, ChangeStatus
正在調用,但獲取空值。 在我誤會的地方幫助我。
表單正在發布到ChangeStatus
,但是沒有表單值被發布。 沒有數據要發布,因為表單沒有實際的表單元素。 因此,該框架沒有價值可用來構造Visit
實例。
由於您僅對記錄執行單個預定義的操作,因此您真正需要的只是該記錄的標識符 。 您不需要整個記錄。 使用該標識符,您將從數據庫中獲取記錄,對其進行更新並保存。
因此,刪除整體表單並為每個表行添加一個表單,然后在其中包含一個用於標識符的表單元素:
<td>
@using (Html.BeginForm("ChangeStatus", "Home", FormMethod.Post))
{
@Html.HiddenFor(modelItem => item.VisitorID)
<input type="submit" value="Change Status" />
}
</td>
(請注意:我猜想 VisitorID
是記錄的標識符。盡管命名並不是100% VisitorID
。使用該記錄存在的任何標識符或一組標識符。)
然后在動作中只需要標識符即可:
[HttpPost]
public ActionResult ChangeStatus(int visitorID)
{
// use the visitorID to fetch the record that you want to update
return RedirectToAction('Index')
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.