首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >MVC5 POST表单从以前的索引中检索数据

MVC5 POST表单从以前的索引中检索数据
EN

Stack Overflow用户
提问于 2016-06-02 06:13:38
回答 2查看 55关注 0票数 0

我用从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

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
// 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");
        }

我能知道哪里出了问题吗?在过去的一个小时里一直试图解决这个问题,只是似乎找不出是什么原因造成的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-02 07:37:11

溶液

更新的Index.cshtml如下。

代码语言:javascript
运行
复制
@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>
                                    }
票数 0
EN

Stack Overflow用户

发布于 2016-06-02 06:19:05

这条线

代码语言:javascript
运行
复制
@using (Html.BeginForm("Patient", "Doctor", new { guid = item.PatientId }, FormMethod.Post, new { id = "submitRequest" })) 

对于所有表单元素,id都是相同的,您将使用相同的id元素通过javascript提交这些表单。所以当你打电话

代码语言:javascript
运行
复制
document.getElementById('submitRequest`)

它将始终返回带有给定id的第一个表单元素。您需要将不同的id属性附加到每个表单元素。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37583988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档