0

我有一个需要填写必填字段的日期选择器文本框。模型:

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

控制组扩展:

     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));
    }

尽管我的代码试图阻止它,但我还是发布到表单。有任何想法吗?

4

2 回答 2

1

尝试删除您的click处理程序。该 javascript 应该是不必要的,因为它应该由 jQuery 验证和Required属性处理。如果一个或多个字段无效,jQuery 验证也会处理取消提交。

于 2013-08-22T14:06:54.960 回答
0

您可以只使用 EditerFor 和 JQUery Datapicker。

更多关于http://blogs.msdn.com/b/stuartleeks/archive/2011/01/25/asp-net-mvc-3-integrating-with-the-jquery-ui-date-picker-and-adding- a-jquery-validate-date-range-validator.aspx

于 2013-08-23T11:09:28.503 回答