我用从Controller传入的模型填充了我的视图。每个循环还将生成一个超链接按钮,应该在guid中传递该按钮。但是,当我单击按钮并启动POST请求时,guid属于前一个索引的按钮。
但是,在使用Chrome的检查工具检查元素之后,按钮上有正确的guid。只是当单击按钮(POSTing)时,作为参数传递的guid属于前一个索引的guid。
例如:
按钮A (GUID 1000)
按钮B (GUID 2000)
按钮C (GUID 3000)
当我单击Button B时,传递给POSTing的guid是1000,它属于按钮A。
Index.cshtml
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.PatientName)
</td>
<td>
@Html.DisplayFor(modelItem => item.NRIC)
</td>
<td>
@using (Html.BeginForm("Patient", "MainController", new { guid = item.PatientId }, FormMethod.Post, new { id = "submitRequest" })) {
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('submitRequest').submit()" onclick="return confirm('A pop up message.');" class="btn btn-success glyphicon glyphicon-plus" title="Request"></a>
}
</td>
</tr>
}
MainController.cs
// POST: Doctor/Patient
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Patient(string guid) {
try {
if (ModelState.IsValid) {
var currentTime = DateTime.Now;
db.Appeal.Add(new Appeal {
DoctorId = User.Identity.GetUserId(),
PatientId = guid, // issue is here.
Time = currentTime,
IsApprove = false
});
TempData["Message"] = "Appeal is being processed. Check the Request page for outcome.";
db.SaveChanges();
return RedirectToAction("Patient");
}
} catch (DataException dex) {
}
return RedirectToAction("Patient");
}
我能知道哪里出了问题吗?在过去的一个小时里一直试图解决这个问题,只是似乎找不出是什么原因造成的。
发布于 2016-06-01 23:37:11
溶液
更新的Index.cshtml如下。
@using (Html.BeginForm("Patient", "Doctor", new { guid = item.PatientId }, FormMethod.Post)) {
@Html.AntiForgeryToken()
<button class="btn btn-success glyphicon glyphicon-plus" type="submit" onclick="return confirm('Are you sure?')"></button>
}
发布于 2016-06-01 22:19:05
这条线
@using (Html.BeginForm("Patient", "Doctor", new { guid = item.PatientId }, FormMethod.Post, new { id = "submitRequest" }))
对于所有表单元素,id
都是相同的,您将使用相同的id
元素通过javascript提交这些表单。所以当你打电话
document.getElementById('submitRequest`)
它将始终返回带有给定id
的第一个表单元素。您需要将不同的id
属性附加到每个表单元素。
https://stackoverflow.com/questions/37583988
复制