从 ASP.NET MVC UI Api 到 kendo DropDownList 的条件逻辑的客户端验证 "required"
Client side validation "required" with conditional logic to kendo DropDownList from the ASP.NET MVC UI Api
我正在为 ASP.NET MVC 使用 kendo UI。我想知道是否有一种方法可以使用 Razor 向 DropDownList 的 HtmlAttributes 添加条件逻辑。从我的示例中可以看出
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate")
.Format("dd/MM/yyyy")
.HtmlAttributes(new { style = "width:100%" })
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我根据我的模型是否有 Id 来设置值。我想知道我的问题是否有语法。可能是这样的
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate")
.Format("dd/MM/yyyy")
.HtmlAttributes(new { style = "width:100%" if(Model.DocumentId){ required = "required" })
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我知道这可以通过 javascript 在元素的数据绑定事件上完成,但我的问题是我的剃刀页面是否有这样做的方法。
Razor 本质上只是视图中的 C# 运行。你在这里试图做的是在一个对象的范围内包装一个 if
语句,即它不会编译。
您能做的最好的事情是将对象值从 HtmlAttributes
方法中移出,然后用 if
语句拆分它:
@{
object myAttributes = null;
if(Model.DocumentId) {
myAttributes = new { style = "width:100%", required = "required" }
}
else {
myAttributes = new { style = "width:100%" }
}
}
然后
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate")
.Format("dd/MM/yyyy")
.HtmlAttributes(myAttributes)
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我对@ChrisC 的回答投了赞成票,因为他帮助我以我需要的方式看待它。我的处理方式是这样的:
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate").Format("dd/MM/yyyy")
.HtmlAttributes(Model.DocumentId == null ? (object) new { style = "width:100%" } : new { style = "width:100%", required = "required" })
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我是这样做的:
@{
dynamic obj = new ExpandoObject();
if (Model.IsRequired)
{
obj.required = "required";
}
obj.style = "width:220px";
}
@(Html.Kendo().TextBoxFor(m => m.Value)
.Enable(!Model.IsDisabled)
.Value(Model.DefaultValue)
.HtmlAttributes(obj)
.Name(Model.Name))
我正在为 ASP.NET MVC 使用 kendo UI。我想知道是否有一种方法可以使用 Razor 向 DropDownList 的 HtmlAttributes 添加条件逻辑。从我的示例中可以看出
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate")
.Format("dd/MM/yyyy")
.HtmlAttributes(new { style = "width:100%" })
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我根据我的模型是否有 Id 来设置值。我想知道我的问题是否有语法。可能是这样的
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate")
.Format("dd/MM/yyyy")
.HtmlAttributes(new { style = "width:100%" if(Model.DocumentId){ required = "required" })
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我知道这可以通过 javascript 在元素的数据绑定事件上完成,但我的问题是我的剃刀页面是否有这样做的方法。
Razor 本质上只是视图中的 C# 运行。你在这里试图做的是在一个对象的范围内包装一个 if
语句,即它不会编译。
您能做的最好的事情是将对象值从 HtmlAttributes
方法中移出,然后用 if
语句拆分它:
@{
object myAttributes = null;
if(Model.DocumentId) {
myAttributes = new { style = "width:100%", required = "required" }
}
else {
myAttributes = new { style = "width:100%" }
}
}
然后
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate")
.Format("dd/MM/yyyy")
.HtmlAttributes(myAttributes)
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我对@ChrisC 的回答投了赞成票,因为他帮助我以我需要的方式看待它。我的处理方式是这样的:
@(Html.Kendo().DatePickerFor(model => model.RequestedDate)
.Name("RequestedDate").Format("dd/MM/yyyy")
.HtmlAttributes(Model.DocumentId == null ? (object) new { style = "width:100%" } : new { style = "width:100%", required = "required" })
.Value(Model.DocumentId != null ? Model.RequestedDate : DateTime.Today)
)
我是这样做的:
@{
dynamic obj = new ExpandoObject();
if (Model.IsRequired)
{
obj.required = "required";
}
obj.style = "width:220px";
}
@(Html.Kendo().TextBoxFor(m => m.Value)
.Enable(!Model.IsDisabled)
.Value(Model.DefaultValue)
.HtmlAttributes(obj)
.Name(Model.Name))