【发布时间】:2017-01-10 21:55:25
【问题描述】:
我是 ASP.NET MVC 的新手。试图从表中的记录列表中获取记录。在每条记录中,我都有一个字段status,该字段设置为1。这里我只显示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中将return View();更改为return RedirectToAction('Index') -
感谢您的回复。我知道。但是在那个 POST 方法中,我必须将状态列从 1 更新到 3。但是我得到 ChangeStatus(Visit visit) 中的所有值都是空的
标签: c# asp.net asp.net-mvc