英文:
How to disable EditorFor item from time to time daily?
问题
To disable the item editor and enable writing only from 4pm to 11:59 pm in an ASP.NET MVC view, you can use the following code:
<div class="form-group">
@Html.LabelFor(model => model.TESTS_EVENING, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@if (DateTime.Now.TimeOfDay >= TimeSpan.FromHours(16) && DateTime.Now.TimeOfDay <= TimeSpan.FromHours(23, 59))
{
@Html.EditorFor(model => model.TESTS_EVENING, new { htmlAttributes = new { @class = "form-control" } })
}
else
{
@Html.TextBoxFor(model => model.TESTS_EVENING, new { @class = "form-control", @readonly = "readonly" })
}
@Html.ValidationMessageFor(model => model.TESTS_EVENING, "", new { @class = "text-danger" })
</div>
</div>
This code checks if the current time is between 4:00 PM and 11:59 PM. If it is, it enables the item editor; otherwise, it makes the editor read-only.
英文:
How can I disable the item Editor for and enable writing only from 4pm to 11:59 pm in an ASP.NET MVC view?
This is the control markup:
<div class="form-group">
@Html.LabelFor(model => model.TESTS_EVENING, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TESTS_EVENING, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TESTS_EVENING, "", new { @class = "text-danger" })
</div>
</div>
How to use:
If Datetime.Now between 16:00 and 23:59
then enable the item, else disable it.
答案1
得分: 1
以下是您要翻译的代码部分:
查看示例中的代码:
@{
TimeSpan start = new TimeSpan(16, 0, 0);
TimeSpan end = new TimeSpan(23, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
}
if ((now > start) && (now < end))
{
@Html.EditorFor(model => model.TESTS_EVENING, new { htmlAttributes = new { @class = "form-control", disabled= "disabled" } })
}
else{
@Html.EditorFor(model => model.TESTS_EVENING, new { htmlAttributes = new { @class = "form-control" } })
}
希望这能帮助您。
英文:
Check this For example in View
@{
TimeSpan start = new TimeSpan(16, 0, 0);
TimeSpan end = new TimeSpan(23, 0, 0);
TimeSpan now = DateTime.Now.TimeOfDay;
}
if ((now > start) && (now < end))
{
@Html.EditorFor(model => model.TESTS_EVENING, new { htmlAttributes = new { @class = "form-control",disabled= "disabled" } })
}
else{
@Html.EditorFor(model => model.TESTS_EVENING, new { htmlAttributes = new { @class = "form-control"} })
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论