2011-09-24 34 views
2

我已經在MVC 3.0 Razor視圖中創建了一個頁面。 Create.cshtml爲什麼我的[HttpPost]方法不會觸發?

@model LiveTest.Business.Models.QuestionsModel 
@*<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) 
    <table cellpadding="1" cellspacing="1" border="0"> 
     <tr> 
      <td>@Html.LabelFor(model => model.TestID) 
      </td> 
      <td> 
       @Html.DropDownListFor(model => model.TestID, (IEnumerable<SelectListItem>)ViewBag.ItemIDList)@Html.ValidationMessageFor(model => model.TestID) 
      </td> 
     </tr> 
     <tr> 
      <td>@Html.LabelFor(model => model.Question) 
      </td> 
      <td>@Html.EditorFor(model => model.Question)@Html.ValidationMessageFor(model => model.Question) 
       @Html.HiddenFor(model => model.QuestionsID) 
      </td> 
     </tr> 
     <tr> 
      <td>@Html.LabelFor(model => model.IsRequired) 
      </td> 
      <td>@Html.CheckBoxFor(model => model.IsRequired)@Html.ValidationMessageFor(model => model.IsRequired) 
      </td> 
     </tr> 
     <tr> 
      <td> 
      </td> 
      <td> 
       <input type="submit" value="Submit" /> 
      </td> 
     </tr> 
    </table> 
} 

QuestionsController.cs

public class QuestionsController : Controller 
    { 
     #region "Attributes" 
     private IQuestionsService _questionsService; 
     #endregion 

     #region "Constructors" 
     public QuestionsController() 
      : this(new QuestionsService()) 
     { 
     } 
     public QuestionsController(IQuestionsService interviewTestsService) 
     { 
      _questionsService = interviewTestsService; 
     } 
     #endregion 
     #region "Action Methods" 
     public ActionResult Index() 
     { 
      return View(); 
     } 
     public ActionResult Create() 
     { 
      InterviewTestsService _interviewService = new InterviewTestsService(); 
      List<InterviewTestsModel> testlist = (List<InterviewTestsModel>)_interviewService.GetAll(); 
      ViewBag.ItemIDList = testlist.Select(i => new SelectListItem() { Value = i.TestID.ToString(), Text = i.Name }); 
      return View(); 
     } 
     [HttpPost] 
     public ActionResult Create(QuestionsModel questions) 
     { 
      if (ModelState.IsValid) 
      { 
       _questionsService.Add(questions); 
       return RedirectToAction("Index"); 
      } 
      InterviewTestsService _interviewService = new InterviewTestsService(); 
      List<InterviewTestsModel> testlist = (List<InterviewTestsModel>)_interviewService.GetAll(); 
      ViewBag.ItemIDList = testlist.Select(i => new SelectListItem() { Value = i.TestID.ToString(), Text = i.Name }); 
      return View(questions); 
     } 
     #endregion 
    } 

QuestionsModel.cs

public class QuestionsModel : IQuestionsModel 
    { 
     [ReadOnly(true)] 
     public Guid QuestionsID { get; set; } 

     [Required] 
     [DisplayName("Question")] 
     public string Question { get; set; } 
     [DisplayName("Test ID")] 
     public Guid TestID { get; set; } 
     [DisplayName("Is Required")] 
     public bool IsRequired { get; set; } 
     [DisplayName("Created By")] 
     public Guid CreatedBy { get; set; } 
      } 

問題:

<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> 

如果我在添加上述兩行Create.cshtml頁面,然後我按下提交按鈕,然後將火驗證消息「是必需的問題!」如果我輸入的值是* 問題字段,然後按提交按鈕我的[HttpPost]Create方法永遠不會執行。 *

如果我刪除從頁上面的兩行,然後按提交按鈕,如果我在問題字段,則也[HttpPost]Create執行輸入值,那麼它會執行從服務器端[HttpPost]Create法和火驗證。

請幫幫我。

+1

我可能會脫節,但你是否包括jquery庫的核心?我只在這裏看到驗證插件,但沒有看到庫。 –

回答

0

QuestionsModel類包括屬性CreatedBy它不包含在您的視圖。

嘗試將CreatedBy作爲隱藏字段添加,或者(更好)從ProblemsModel類中刪除CreatedBy,因爲它不是應該在視圖中公開的屬性。

我懷疑這個缺失的屬性是問題的原因。

UPDATE

我跑在你的代碼的一些測試,它不是CreatedBy屬性。相反,您的問題是您沒有提供QuestionsID值,但是您在表單上包含了一個用於QuestionsID的隱藏字段。

由於QuestionsID是一個值類型,因此DataAnnotationsModelValidatorProvider默認將一個Required驗證程序添加到QuestionsID字段。由於該字段沒有ValidationMessage,因此您看不到驗證錯誤。

您可以按照my answer here中的說明覆蓋默認DataAnnotationsModelValidatorProvider的行爲。

+0

它不應該導致它。 – tugberk

+0

不,這是不是問題的原因,因爲我有幾個其他形式,其中包含一些字段,我沒有在我的視圖中使用,但仍然工作。 – imdadhusen

0

我會檢查嘗試提交表單時是否發生任何客戶端錯誤。從瀏覽器控制檯檢查它。

此外,在提交表單之前,請確保您已完成沒有驗證錯誤的代碼。

+0

沒有表格驗證客戶端工作正常,但如果我填寫所有必填字段,然後按提交按鈕,然後它不會得到回傳。即使我沒有在瀏覽器中得到任何驗證錯誤 – imdadhusen

0

你是說窗體不驗證客戶端,也沒有任何東西被髮回到你的服務器?

意思是,你點擊提交按鈕,瀏覽器中沒有任何事情發生,對不對?

問題可能是您的表單未通過不顯眼的JavaScript庫驗證進行驗證。

+0

沒有表單驗證客戶端工作正常,但如果我填寫所有必填字段,然後按提交按鈕,然後它不會得到回傳。即使我沒有在瀏覽器中得到任何驗證錯誤 – imdadhusen

相關問題