2012-06-12 51 views
0

我有一個視圖如下:DemoView.cshtmlHttpPost如果提交按鈕被禁用方法沒有燒製

@model Mvc3Razor.Models.UserModel 
@{ 
    ViewBag.Title = "Create"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 
<h2> 
    Create</h2> 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 
@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 
    <fieldset> 
     <legend>UserModel</legend> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.UserName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.UserName) 
      @Html.ValidationMessageFor(model => model.UserName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.FirstName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.FirstName) 
      @Html.ValidationMessageFor(model => model.FirstName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.LastName) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.LastName) 
      @Html.ValidationMessageFor(model => model.LastName) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.City) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.City) 
      @Html.ValidationMessageFor(model => model.City) 
     </div> 
     <p> 
      <input type="submit" value="Create" /></p> 
    </fieldset> 
} 
<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 
<script type="text/javascript"> 
    $(function() { 
     $('input[type="submit"]').click(function() { 
      $(this).attr('disabled', 'disabled'); 
     }); 
    }); 
</script> 

以及控制器:HomeController.cs

[HttpPost] 
     public ViewResult Create(UserModel um) 
     { 
      if (!TryUpdateModel(um)) 
      { 
       ViewBag.updateError = "Create Failure"; 
       return View(um); 
      } 

      _usrs.Create(um); 
      return View("Details", um); 

     } 

爲了防止多個按鈕點擊,我試圖禁用按鈕,一旦用戶點擊創建按鈕,但一旦創建按鈕被禁用它不會觸發HttpPost方法。

任何人都可以幫我解決這個問題嗎?

回答

4

嘗試禁用按鈕提交表單時,不被點擊提交按鈕時:

$("form").submit(function() { 
    $(this).attr("disabled", "disabled"); 
    return true; 
}); 
0

我會建議是在全成後重定向用戶返回到索引頁。你想使用PRG Pattern

Post, Redirect, Get (PRG) pattern在它是「幫助避免 重複的表單提交,並允許Web應用程序與瀏覽器書籤和刷新按鈕表現得更加 直觀」

usrs.Create(um); 
//Redirect to Index Page here 

SO question欲瞭解更多詳情

相關問題