Basit Nisar

Basit Nisar

  • 1.7k
  • 84
  • 11k

How to add an custom message in our view model class

Jul 12 2023 8:10 AM
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


Answers (1)