2013-10-19 100 views
2

我在我的mvc項目中有一些驗證。但是,當我sumbit一個空的表單或表單中沒有輸入一些必填字段時,它不會保留在相同的表單/視圖中並顯示錯誤。我看不到任何模式錯誤(需即量字段)MVC查看不顯示驗證錯誤

例如,這裏有一些屬性,我的語氣

[Required] 
    [StringLength(1, MinimumLength = 1)] 
    public string Period { get; set; } 

    [Required] 
    [DataType(DataType.DateTime)] 
    public System.DateTime ModifyDate { get; set; } 

這裏是我的控制器

[HttpPost] 
     public ActionResult StopScheduled([Bind(Prefix = "item")] BillPayModel model) 
     { 
      //getUsers(); 

      try 
      { 
       if (ModelState.IsValid) 
       { 

        //save stuff into db 
        db.SaveChanges(); 

       } 
       else 
       { 
        ModelState.AddModelError("", "Could not Stop Scheduled Payment"); 
       } 
      } 


      catch (FormatException) 
      { 
       ModelState.AddModelError("", "Could not Stop Scheduled Payment"); 
      } 


      return RedirectToAction("StopScheduled"); 
     } 

    } 

這裏是我的看法

@if (Model !=null) 
{ 
    if (Model.IsSuccess == true) 
    { 
     <span><center><font color = "green">Successfully Completed Transaction! </font></center></span> 
    } 
    else 
    { 
     @Html.ValidationSummary(true, "ERROR! Please make sure you have entered correct details"); 
    } 
} 

@if (Model ==null) 
{ 
    using (Html.BeginForm("BillPay", "BillPay", FormMethod.Post, new {})) 
    { 
     @Html.ValidationSummary(true); 

     <div>@Html.LabelFor(model => model.AccountNumber)</div> 
     @Html.DropDownList("Accounts", "-- Select User --") 

     <div>@Html.LabelFor(model => model.PayeeID)</div> 
     @Html.DropDownList("PayeeID", "-- Select User --")   


     <div>@Html.LabelFor(model => model.Amount)</div> 
     <div>@Html.TextBoxFor(model => model.Amount,new {style = "width:150px"}) 
      @Html.ValidationMessageFor(model => model.Amount) 
     </div>   

     <div>@Html.LabelFor(model => model.ScheduleDate) (i.e 20/10/2013 10:00)</div> 
     <div>@Html.TextBoxFor(model => model.ScheduleDate,new {style = "width:250px"}) 
      @Html.ValidationMessageFor(model => model.ScheduleDate) 
     </div> 

     <div>@Html.LabelFor(model => model.Period)</div> 
     <div>@Html.TextBoxFor(model => model.Period,new {style = "width:150px"}) 
      @Html.ValidationMessageFor(model => model.Period) 
     </div> 

     <input type="submit" value ="Submit" style="width:8%;height:5%"/> 
     <input type="reset" value ="reset" style="width:8%;height:5%"/> 
    } 
} 
else 
{ 

} 
+1

您是否在您(主)頁面中包含了'jquery.validation.js'? – Silvermind

+0

這是我的想法:D – Krekkon

回答

2

問題是你正在用RedirectToAction做一個新的請求,所以驗證您的模型設置爲有效。爲了解決你的問題,你需要做的是這樣的:

[HttpPost] 
public ActionResult StopScheduled([Bind(Prefix = "item")] BillPayModel model) 
{ 
     try 
     { 
      if (ModelState.IsValid) 
      { 
       //save stuff into db 
       db.SaveChanges(); 
      } 
      else 
      { 
       ModelState.AddModelError("", "Could not Stop Scheduled Payment"); 
      } 
     } 
     catch (FormatException) 
     { 
      ModelState.AddModelError("", "Could not Stop Scheduled Payment"); 
     } 
     return View(model); 
} 

並在視圖你應該改變Html.ValidationSummary excludePropertyErrors爲false,像這樣:

if (Model.IsSuccess == true) 
{ 
    <span><center><font color = "green">Successfully Completed Transaction! </font></center></span> 
} 
else 
{ 
    @Html.ValidationSummary(false, "ERROR! Please make sure you have entered correct details"); 
} 

向他們展示你需要oneach輸入yoo改變你的if語句(Model == null)。

+0

這沒有真正解決它。我仍然得到的是「錯誤!請確保你輸入了正確的細節」。當我提交這個錯誤時,爲什麼我沒有被顯示? – kayze

+0

@kayze是ofc它只顯示,因爲如果模型不爲null你不顯示你的驗證消息。 – Rikard

+0

@kayze se我的更新來解決您的問題 – Rikard