2013-08-22 83 views
0

我有一個datepicker文本框,我需要做一個必填字段。 型號:MVC4驗證與引導datePicker

[Required(AllowEmptyStrings = false, ErrorMessage = "Required")] 
    [DisplayFormat(ConvertEmptyStringToNull = false)] 
    [Display(Name = "Date of Something")] 
    [DatePicker] //custom tag builder I wrote to make this a datepicker 
    public string DateOfSomething { get; set; } 

查看:

@Html.ControlGroupFor(x => x.DateOfSomething) //has the custom tag I built to handle datepicker 
    @Html.ValidationMessageFor(x => x.DateOfSomething) 

的jQuery:

$("#btnSubmit").click(function() { 
     if ($('#DateOfSomething') == "" || $('#DateOfSomething') == null) { 
      $("#ValidationSummary").append("<div>Required</div>"); 
      $("#ValidationSummary").show(); 
     } 
    }); 

在_layout

<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> 
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive-custom-for-bootstrap.js")"></script> 

ControlGroupFor擴展:

 public static MvcHtmlString ControlGroupFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression) 
    { 
     var propertyName = ExpressionHelper.GetExpressionText(expression); 
     var controlGroupWrapper = BuildTagWith("div", "control-group"); 
     controlGroupWrapper.InnerHtml += htmlHelper.LabelFor(expression, new Dictionary<string, object> { { "class", "control-label" } }).ToHtmlString(); 
     var controlDiv2 = BuildTagWith("div", "controls"); 

     var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 
     var inputTag = ""; 

     var valAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(propertyName, metadata); 

     if (metadata.AdditionalValues.ContainsKey("DatePicker")) 
     { 
      CreateDatePicker(htmlHelper, expression, propertyName, controlDiv2, controlGroupWrapper); 
      controlGroupWrapper.InnerHtml += htmlHelper.ValidationMessageFor(expression, null, valAttributes); 
      return MvcHtmlString.Create(controlGroupWrapper.ToString(TagRenderMode.Normal)); 
     } 
     if (metadata.AdditionalValues.ContainsKey("DropDownListPropertyName")) 
     { 
      var propertyNameForOptions = metadata.AdditionalValues["DropDownListPropertyName"] as string; 
      var options = GetPropValue(htmlHelper.ViewData.Model, propertyNameForOptions) as Dictionary<string, string>; 
      var selectedItem = GetPropValue(htmlHelper.ViewData.Model, propertyName) as string; 
      inputTag += BuildSelectList(options, propertyName, selectedItem); 
     } 
     else 
     { 
      var value = GetPropValue(htmlHelper.ViewData.Model, propertyName); 
      inputTag += htmlHelper.EditorFor(expression).ToHtmlString(); 
     } 
     controlDiv2.InnerHtml += inputTag + htmlHelper.ValidationMessageFor(expression, null, valAttributes); 

     controlGroupWrapper.InnerHtml += controlDiv2.ToString(TagRenderMode.Normal); 

     return MvcHtmlString.Create(controlGroupWrapper.ToString(TagRenderMode.Normal)); 
    } 

儘管我的代碼試圖阻止它,但我發佈到窗體。有任何想法嗎?

+0

有什麼特別的原因,你的'DateOfSomething'屬性是'string'而不是'DateTime'? – asymptoticFault

+0

不是真的,考慮到MVC將所有內容都轉換爲視圖中的字符串我只是將其轉換回日期時間以後 – Robert

+0

強烈地鍵入模型雖然提供了許多優點,即在這種情況下它會給你更嚴格的驗證。當然,視圖中的值是「字符串」,但是當它們被回發時,MVC模型綁定會自動驗證並將它們轉換爲它們的目標類型,這是一個巨大的優勢。 – asymptoticFault

回答

1

嘗試刪除您的click處理程序。該javascript應該是不必要的,因爲它應該由jQuery驗證和Required屬性來處理。如果一個或多個字段無效,jQuery驗證也會處理取消提交。

+0

這確實更有意義,但表單仍然提交 – Robert

+0

我沒有捆綁任何東西,但我試圖使用內置的MVC驗證,但沒有運氣。在我的編輯中,我有所有必需的文件來獲得我的驗證 – Robert

+0

完成和現在相同的問題,我只有:[DatePicker] [Display(Name =「Date of Something」)] [Required(ErrorMessage =「Required」)) ] public string DateOfSomething {get;組; } – Robert