我有一個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));
}
儘管我的代碼試圖阻止它,但我發佈到窗體。有任何想法嗎?
有什麼特別的原因,你的'DateOfSomething'屬性是'string'而不是'DateTime'? – asymptoticFault
不是真的,考慮到MVC將所有內容都轉換爲視圖中的字符串我只是將其轉換回日期時間以後 – Robert
強烈地鍵入模型雖然提供了許多優點,即在這種情況下它會給你更嚴格的驗證。當然,視圖中的值是「字符串」,但是當它們被回發時,MVC模型綁定會自動驗證並將它們轉換爲它們的目標類型,這是一個巨大的優勢。 – asymptoticFault