locked
how can i make the datetime column set to empty in view RRS feed

  • Question

  • User-1355965324 posted

    when the view is loading , the tax date column is always showing with the value '01/01/0001 00:00:00' untill  the date selected from datepicker. How can I set the datetime value set to null when the view is loaded.

    This is model

    public class Vehicles
        {
            [Key]
            public int Id { get; set; }
            public string Make { get; set; }
            public string Model { get; set; }
            public string RegNo { get; set; }
            public string VehicleName {get;set;}
            public DateTime TaxDate { get; set; }
            public DateTime RegDate { get; set; }         
           
        }

    controller

     public async Task<IActionResult> UpsertVehicle(int? vehicleId)
            {
    
    
                VehicleVM vehiclesVM = new VehicleVM();
               
               
                return View(vehiclesVM);
            }
    

    view

    <form asp-controller="Vehicle"
          asp-action="UpsertVehicle"
          data-ajax="true"
          data-ajax-method="POST"
          data-ajax-mode="replace"
          data-ajax-update="#content"
          class="form-horizontal"
          role="form" data-parsley-validate novalidate>
        <div class="alert-danger" asp-validation-summary="ModelOnly"></div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group row">
                    <label asp-for="Vehicles.Make" class="control-label  col-3 col-form-label">Make</label>
                    <div class="col-9">
                        <input class="form-control input-sm" type="text" id="inputName"
                               asp-for="Vehicles.Make" />
                    </div>
                </div>
            </div>
       
        <div class="row">
           
            <div class="col-md-6">
                <div class="form-group row">
                    <label asp-for="Vehicles.TaxDate" class="control-label col-3 col-form-label">Tax Date</label>
                    <div class="col-9">
                        <input class="form-control input-sm" type="text" id="TaxDate"
                               asp-for="Vehicles.TaxDate" />
                        <span asp-validation-for="Vehicles.TaxDate" class="text-danger"></span>
                    </div>
                </div>
            </div>
        </div>
       
    
    </form>

    Tuesday, June 16, 2020 7:24 AM

Answers

  • User-217098811 posted

    Hi polachan

    I think you should make it nullable when setting properties

    Like this

    public class Vehicles
        {
            [Key]
            public int Id { get; set; }
            public string Make { get; set; }
            public string Model { get; set; }
            public string RegNo { get; set; }
            public string VehicleName {get;set;}
            public DateTime? TaxDate { get; set; }
            public DateTime? RegDate { get; set; }         
           
        }

    Hope this can help you

    Best Regards

    yinqiu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 16, 2020 9:08 AM

All replies

  • User2078676645 posted

    Hi,

    Your program is working well with me. There is nothing in this text box. It may be a problem with datepicker. Please check other related codes.

    Regards,

    Evern

    Tuesday, June 16, 2020 8:40 AM
  • User-217098811 posted

    Hi polachan

    I think you should make it nullable when setting properties

    Like this

    public class Vehicles
        {
            [Key]
            public int Id { get; set; }
            public string Make { get; set; }
            public string Model { get; set; }
            public string RegNo { get; set; }
            public string VehicleName {get;set;}
            public DateTime? TaxDate { get; set; }
            public DateTime? RegDate { get; set; }         
           
        }

    Hope this can help you

    Best Regards

    yinqiu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 16, 2020 9:08 AM