MVC不止一次提交表单

时间:2016-05-26 04:59:12

标签: c# asp.net-mvc forms asp.net-ajax

我遇到了一个奇怪的问题,我希望有人可以解释为什么会发生以下情况。

我的控制器:

MasterModel main = new MasterModel(); 

public ActionResult Index()
{
    return View();
}

[HttpGet]
public ActionResult VehicleDetail()
{
    pageSessionSetup();
    return PartialView("VehicleDetail", main.Vehicle);
}

[HttpPost]
public ActionResult VehicleDetail(VehicleDetailDisplay model)
{
    ModelState.AddModelError("", "Errors occured");
    main.Vehicle = model;
    pageSessionSetup();
    return PartialView("VehicleDetail", main.Vehicle);
}

更新了视图

我的观点:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

@using (Ajax.BeginForm("VehicleDetail", "Operator", null, new AjaxOptions
{
   UpdateTargetId = "VehicleDetail",
   InsertionMode = InsertionMode.Replace,
   HttpMethod = "Post"}, new { id = "VehicleDetail" }))
{
   @Html.Partial("_ValidationSummary", ViewData.ModelState)
   <div class="panel panel-default panel-body">
       ...
   </div>

   <div class ="col-lg-7 col-md-7 col-sm-7 col-xs-7 row">
       <button type="submit" value="Save" class="btn btn-lg btn-green col-lg-2 col-md-5 col-sm-7 col-xs-7">Save</button>
   </div>
}

在我的部分视图中,我有一个提交按钮,但是当我单击提交按钮一次时,表单会被提交多次。

1 个答案:

答案 0 :(得分:5)

UPDATE:

使用Html.BeginForm("ActionName", "ControllerName", FormMethod.Post)代替Ajax.BeginForm() 检查您是否在网页中包含jquery.unobtrusive-ajax.min.js两次(部分视图中)。< / p>

----------------------------------------------- -------------------------------------------------- -------

@using (Html.BeginForm("ActionName", ""ControllerName", FormMethod.Post))
{    
    <label>SomeLabel</label>
    ...
    <input type="submit" value="Button" /></p> 
}