我正在使用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来完成,但我的问题是在剃刀页面中是否有这样做的方法。
答案 0 :(得分:2)
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)
)
答案 1 :(得分:1)
我赞成了@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)
)
答案 2 :(得分:1)
我已经这样做了:
df2 <- data.frame(group = 1:3, xval = c(1.5, 2.5, 3.2))
newData <- data.frame(group=c(rep(1,5),rep(2,5),rep(3,5),rep(4,5)),
xval = rep(c(0,1,2,3,4),4))