how to add an error message for the datetime property in c# .net

Basit Nisar 40 Reputation points
2023-07-12T08:33:20.09+00:00
public class FloodViewModel

    {

       // public string ClaimId { get; set; }

        public string ClientClaimNo { get; set; }

        public string ProvinceCode { get; set; }

        public DateTime? LossDate { get; set; }

        public string EmployeeNumber { get; set; }

        public string ClaimClosureType { get; set; }

        public string AdjusterName { get; set; }

        public string GrossBuildingAmount { get; set; }

        public string GrossDetachedGarageAmount { get; set; }

        public string BuildingCoverageAmount { get; set; }

        public string LAEAmount { get; set; }

        public DateTime? CompletedDate { get; set; }

        public string Comments { get; set; }

    }

    public class FloodListViewModel

    {

        public string FileName { get; set; }

        public List<FloodViewModel> FloodViewModels { get; set; }

    }

 

{
    
            ClaimsPrincipal principal = Request.GetRequestContext().Principal as ClaimsPrincipal;
            Int32 LogedInID = Convert.ToInt32(principal.Claims.FirstOrDefault(t => t.Type == "LogedInID").Value);


                    if (!ModelState.IsValid)
                {
                

                    var ErrorList = ModelState.Where(x => x.Value.Errors.Any()).ToDictionary(t => t.Key.Split('.').Last(), t => (string.IsNullOrEmpty(t.Value.Errors[0].ErrorMessage) ? t.Value.Errors[0].Exception.Message : t.Value.Errors[0].ErrorMessage));
                    return this.ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new { Status = Global.Status.Invalid.ToString(), Message = ErrorList }));

                }
                
                
            

            
            if (ListFloodView.FloodViewModels != null && ListFloodView.FloodViewModels.Count > 0)
            {
                #region Bulk FloodData Upload process
                try
                {
                    using (var context = new CATConnection_DevEntities())
                    {
                        using (var conn = db.Database.Connection)
                        {
                            conn.Open();
                            var FloodDT = Global.ConvertToDataTable(ListFloodView.FloodViewModels.ToList());
                            var command = conn.CreateCommand();
                            command.CommandText = "[dbo].[InsertFloodFile]";
                            command.CommandType = CommandType.StoredProcedure;
                            command.CommandTimeout = 300;
                            var parameterLogedInID = new SqlParameter("@LogedInID", LogedInID);
                           
                            using (var reader = command.ExecuteReader())
                            {
                            }
                        }
                    }
                    return this.ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new
                    {
                        Status = Global.Status.OK.ToString(),
                        Message = Global.StatusMessage.Created.ToString()
                    }));
                }
                catch (Exception ex)
                {
                    Global.InsertException(ex);
                    return this.ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new { Status = Global.Status.NotFound.ToString(), Message = Global.StatusMessage.NotFound }));
                }
                #endregion              
            }

i basically want to set an error message for lossDate and CompletedDate property if user sets  string value in this datetime field

 
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2023-07-13T03:27:36.41+00:00

    Hi @Basit Nisar,

    You can use model validation to specify a custom display format for field values through the DisplayFormat property.

     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
     public DateTime? LossDate { get; set; }
    

    User's image

    Best regards,

    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments