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 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
Mohamed Azarudeen ZPosted Jul 12, 2023, 3:16 PM
Hi Basit
First, import the
System.ComponentModel.DataAnnotationsnamespace to access the validation attributes. Then, modify yourFloodViewModelclass as follows:By adding the
ErrorMessageproperty to theDisplayattribute forLossDateandCompletedDate, you can customize the error message to be displayed when the validation fails.Then, when validating the model in your controller action, you can check the
ModelStatefor any errors and retrieve the custom error messages:If you find this answer usefull kindly accept thanks.