如何从MVC中的表单验证重复的发帖请求

时间:2018-08-29 12:39:58

标签: asp.net-mvc

我有一个表单,每当发出过帐请求时,都会插入一条记录,但是问题是,如果某人单击提交按钮的次数比重复过帐的请求多于1次,最后插入了相同的记录。我不想检查记录是否已经存在,因为记录总是不同的。我尝试在控制器中使用ValidateAntiForgeryToken过滤器,但无法验证请求,以下是我的查看代码。

@using (Html.BeginForm("Create", "Home",FormMethod.Post,new { onkeydown = "return event.keyCode!=13" }))
{
@Html.AntiForgeryToken()
<div class="right-col">
     @Html.TextBoxFor(model => model.Name, new { placeholder = "Name", @class = "small-box" })
 </div>
 <div class="left-col">Email Id :</div>
  <div class="right-col">
     @Html.TextBoxFor(model => model.EmailId, new { placeholder = "Email Id", @class = "small-box",id="resumeemailid" })
     @Html.ValidationMessageFor(model => model.EmailId)
  </div>
  <div class="left-col">Address :</div>
  <div class="right-col">
      @Html.TextAreaFor(model => model.Address, new { placeholder = "Address", @class = "small-box" })
  </div>
  <div class="buttons resume-threebutton">
      <input type="submit" id="register-button" class="gradient-btn" value="@T("Account.Passport.Register.Button.Upload", "Upload")" name="Command" />
       <input type="submit" id="register-button" class="gradient-btn" value="@T("Account.Passport.Button.UploadandAdd", "Upload And Add New")" name="Command" />
       <input type="button" id="register-button" class="gradient-btn" value="@T("Account.Passport.Register.Button.Cancel", "cancel")" name="register-button" onclick="location.href='@Url.Action("SelectTemplate", "CustomerTemplate")'" />
 </div>
}

和下面是我的控制器Post方法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ProductModel model)
{
     //To do add the product here...
}

0 个答案:

没有答案