2012-06-29 32 views
1

我有一個選項卡內呈現選項卡控件,看起來像這樣的局部視圖內的一種形式:數據註釋驗證未在ASP.NET MVC工作3

@model USARAFSyncMVC.Areas.Event.Models.EventFullScaffoldModel 

@using (Ajax.BeginForm("SaveMainEventDetails", "Event", new { area = "Event" }, 
    new AjaxOptions { UpdateTargetId = "FormWrapper", OnComplete = "SetSuccessLabel", InsertionMode = InsertionMode.Replace }, 
    new { method = "post" })) 
{ 
    @Html.Hidden("eventType", "1", new { id = "eventType" }) 
    <div id="FormWrapper"> 
     <hr /> 
     <table border="0"> 
     <tr> 
      <td>Title</td> 
      <td>@Html.TextBoxFor(model => model.Title, new { style = "width:300px" }) 
      @Html.ValidationMessageFor(model => model.Title)</td> 

     </tr> 
     <tr> 
      <td>OPR</td> 
      <td> @Html.EditorFor(model => model.OPRID)</td> 

     </tr> 
     <tr> 
      <td>Organization</td> 
      <td> @Html.EditorFor(model => model.DomainID)</td> 

     </tr> 
     <tr> 
      <td>POC</td> 
      <td> @Html.EditorFor(model => model.POC)</td> 

     </tr> 
     <tr> 
      <td>Location</td> 

      <td> 
      @Html.EditorFor(model => model.LocationID) 
      </td> 

     </tr> 
     <tr> 
      <td>Dates</td> 
      <td> 
      <table> 
       <tr> 
       <th>@Html.LabelFor(m => m.StartDate)</th> 
       <th> @Html.LabelFor(m => m.EndDate)</th> 
       </tr> 
       <tr> 
       <td> @Html.EditorFor(m => m.StartDate)</td> 
       <td>@Html.EditorFor(m => m.EndDate)</td> 
       </tr> 

      </table> 

      <table id="DeployRedeployDiv"> 
       <tr> 
       <th>Deploy</th> 
       <th>ReDeploy</th> 
       </tr> 
       <tr> 
       <td> @Html.EditorFor(m => m.EstimatedDeployDate)</td> 
       <td>@Html.EditorFor(m => m.EstimatedReDeployDate)</td> 
       </tr> 

      </table> 

      </td> 
     </tr> 
     <tr> 
      <td>OSRs</td> 
      <td> 
      @(Html.Telerik() 
        .PanelBar() 
        .Name("PanelBar") 
        .HtmlAttributes(new { style = "width:300px;" }) 
        .Items(o => o.Add().Text("Click").Content(@<text> 

       @Html.CheckBoxList("OprList", 
        x => x.OprList, 
        x => x.OPRID, 
        x => x.AltTitle, 
        x => x.EventOSRs, Position.Vertical)</text>).Expanded(false))) 
      </td> 
     </tr> 
     <tr> 
      <td>Purpose</td> 
      <td> @Html.TextBoxFor(model => model.Why, new { style = "width: 400px; height:200px" })</td> 
     </tr> 
     <tr> 
      <td>Background</td> 
      <td> @Html.TextBoxFor(model => model.What, new { style = "width: 400px; height:200px" })</td> 
     </tr> 
     <tr> 
      <td>Viewable</td> 
      <td> @Html.CheckBoxFor(model => model.Viewable)</td> 
     </tr> 

     </table> 
     <div> 
     <input class="t-button" type="submit" name="button" value="Save" /> 

     </div> 

    </div> 

} 

這裏是控制器:

[HttpPost] 
    public ActionResult SaveMainEventDetails(EventFullScaffoldModel model, string[] OprList, string eventType, string cbLoc, string cbOpr, string cbOrg) 
    { 
     if (OprList != null) 
     { 
      model.EventOSRs = miscRepository.GetOprsList().ToModel().Where(o => OprList.Contains(o.OPRID.ToString())).ToList(); 
     } 

     model.Type = int.Parse(eventType); 
     model.LocationID = cbLoc; 
     model.OPRID = int.Parse(cbOpr); 
     model.DomainID = int.Parse(cbOrg); 
     eventRepository.Insert(model.ToDto()); 
     return View(); 
    } 

這裏是型號:

public partial class EventFullScaffoldModel 
    { 
     public Int32 EventID { get; set; } 

     [Required(ErrorMessage = "Required!")] 
     public String Title { get; set; } 

     [Required(ErrorMessage = "Required!")] 
     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")] 
     public DateTime StartDate { get; set; } 

     [Required(ErrorMessage = "Required!")] 
     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")] 
     public DateTime EndDate { get; set; } 

     [Required(ErrorMessage = "Required!")] 
     public String What { get; set; } 

     [Required(ErrorMessage = "Required!")] 
     public String Why { get; set; } 

     public Nullable<DateTime> Modified { get; set; } 

     public String ModifiedBy { get; set; } 

     public Nullable<DateTime> Created { get; set; } 

     public String CreatedBy { get; set; } 

     [UIHint("ActiveDirectoryLoadOnDemand"), Required] 
     public String POC { get; set; } 

     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}")] 
     public Nullable<DateTime> EstimatedDeployDate { get; set; } 

     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:MM-dd-yyyy}")] 
     public Nullable<DateTime> EstimatedReDeployDate { get; set; } 

     public Nullable<Int32> TSCMISID { get; set; } 

     public Nullable<Int32> ReviewStatus { get; set; } 

     public int? Type { get; set; } 

     public String InactivityReason { get; set; } 

     public Boolean Viewable { get; set; } 

     public string OPRAltTitle { get; set; } 

     [UIHint("OprDropDown"), Required] 
     public int OPRID { get; set; } 

     [UIHint("OrgDropDown"), Required] 
     public int DomainID { get; set; } 

     [UIHint("LocationsLoadOnDemand"), Required] 
     public string LocationID { get; set; } 

     public string LocationTitle { get; set; } 

     public IList<OsrModel> EventOSRs { get; set; } 

     public IList<TargetAndEffectModel> EventTargetEffects { get; set; } 

     public IList<AssociationModel> EventAssociations { get; set; } 

     public IList<EventObjectiveModel> EventObjectives { get; set; } 

     public IList<StrategicObjectiveModel> EventTSOs { get; set; } 

     public IList<TaskModel> EventTasks { get; set; } 

     public IList<PaxModel> EventPaxBreakDowns { get; set; } 

     public IList<FundingModel> EventFundings { get; set; } 

     public IList<UnitModel> EventExecutingUnits { get; set; } 

     public IList<OsrModel> OprList { get; set; } 

     public IList<ObjectiveModel> ObjectiveList { get; set; } 

     public IList<StrategicObjectiveModel> StrategicList { get; set; } 

     public IList<OrgModel> OrgsList { get; set; } 
    } 

示範從來沒有得到驗證,並直接通過TRA運行控制器上的操作。爲什麼這不起作用?

回答

4

我建議首先檢查您是否已經包含在右爲了不引人注目的驗證所需的JavaScript文件,這將使驗證thsi鏈接在客戶端成功發生。

關於服務器端驗證與其他告訴基本上你必須通過明確地檢查ModelState.IsValid保存模型到數據庫。由於您正在進行AJAX調用,因此我建議您將模型狀態錯誤返回爲JSON。

因此,在AjaxOptionsOnFailure方法中,您可以解析JSON並將錯誤顯示爲div中的摘要。

基本上你可以按照這種模式。

[HttpPost] 
public JsonResult SaveMainEventDetails(..) 
{ 
    if(ModelState.IsValid) 
    { 
    .. save to database 

    return Json(new{ success = true }); 
    } 

    var errorDict = ModelState.. 
    return Json(new { success = false, errors = errorDict }); 
} 
2

你需要明確檢查是否ModelState.IsValid的動作,並返回到編輯視圖,如果它不是以觸發模型驗證「T。
如果您顯示無效模型的編輯視圖,則MVC將通過驗證幫助器自動顯示錯誤消息。

+0

我是否返回部分視圖,因爲這是傳遞模型? –

+0

好吧,我只是試過這個,它沒有工作。在Model.IsValid返回false後返回模型的部分視圖。 –

0

在查看

添加@{ Html.EnableClientValidation(); },並參考有關詳細信息,How to: Validate Model Data Using DataAnnotations Attributes

+0

沒有,也沒有工作。它可能是Ajax的東西嗎?該模型正在驗證,但返回時它不會顯示錯誤 –

+0

您是否使用了正確的所需Js文件? jquery.unobtrusive-ajax.js,jquery.validate.unobtrusive.js – swapneel

0

好的問題是Telerik Script Registrar沒有引用正確的JQuery。現在它正在工作。我直接引用了JQuery庫,也是驗證腳本。在Telerik腳本註冊服務器上,它已經是默認組的一部分,重新​​引用它們,導致它出於某種原因而嚇壞了。只添加對原始默認組之外的腳本的引用。有關更多信息,需要閱讀此MVC擴展的在線文檔。