ASP.NET MVCを初めて使用しています。テーブルのレコードのリストからレコードを取得しようとしています。各レコードには、status
というフィールドがあり、1に設定されています。ここでは、View
とButton
のレコードが1つだけ表示されています。ボタンをクリックすると、そのレコードのステータスをデータベースで3に変更し、同じビューで別のレコードを取得する必要があります。現在私はScaffolding template Listを選択してこのビューを作成しました。ASP.NET MVCのテーブルの特定の列を変更します。
コントローラー:
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
は、呼び出しが、NULL値を得ています。私が間違っているところで助けてください。
'ChangeStatus'であなたの' Return View(); 'をRedirectToAction( 'Index')を返すように変更してみてください。 – techspider
返事ありがとうございます。そんなこと知ってる。しかし、そのPOSTメソッドでは、ステータス列を1から3に更新する必要があります。しかし、** ChangeStatus(Visit visit)ですべての値がnullになっています** – NNR