0

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ziad Adnan
  • 710
  • 5
  • 18
  • Do you mean that the `DateTime.Now` is the current time on the client computer? Take to account the `DateTime.Now` is the c# property and the c# code is running on the server side. – Jackdaw May 06 '23 at 11:40
  • @Jackdaw I need to check local country time the system will used in more than one computer so I need to use it depends on Saudi Arabia local time – Ziad Adnan May 06 '23 at 11:45

1 Answers1

1

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"} })
    
    }
  • thank you so much its working :) I need your help also if there is any issue in coming days – Ziad Adnan May 06 '23 at 12:24
  • can you help me brother in this error how to solve it please or if you have another way how to insert multiple rows into database https://stackoverflow.com/questions/76337895/why-i-got-error-value-cannot-be-null-parameter-name-source – Ziad Adnan May 27 '23 at 04:12