如何在每天的特定时间禁用EditorFor项目?

huangapple go评论46阅读模式
英文:

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:

&lt;div class=&quot;form-group&quot;&gt;
    @Html.LabelFor(model =&gt; model.TESTS_EVENING, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
    &lt;div class=&quot;col-md-10&quot;&gt;
        @Html.EditorFor(model =&gt; model.TESTS_EVENING, new { htmlAttributes = new { @class = &quot;form-control&quot; } })
        @Html.ValidationMessageFor(model =&gt; model.TESTS_EVENING, &quot;&quot;, new { @class = &quot;text-danger&quot; })
    &lt;/div&gt;
&lt;/div&gt;

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 &gt; start) &amp;&amp; (now &lt; end))
    {
               @Html.EditorFor(model =&gt; model.TESTS_EVENING, new { htmlAttributes = new { @class = &quot;form-control&quot;,disabled= &quot;disabled&quot; } })
 
    
    }
    else{
       @Html.EditorFor(model =&gt; model.TESTS_EVENING, new { htmlAttributes = new { @class = &quot;form-control&quot;} })
    
    }

huangapple
  • 本文由 发表于 2023年5月6日 17:25:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188132.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定