I have a WepApi that is call from a Angular/Json client that serializes to model created in EF code first. I would like to post the product model and set the changedate property inside the post/save function. I get ModelState.IsValid = false, is there any work around I want to keep my ef model property
[Required]
public DateTime ChangeDate { get; set; }
get this error tray posting
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Property 'ChangeDate' on type 'Entity.Product' is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract]
CODE model
[DataContract]
public class Product
{
public Product()
{
ChangeDate = DateTime.Now;
}
//Serialization
[DataMember]
//DataAnnotations
[DisplayName("ProductId")]
[Description("ProductId")]
public int ProductId { get; set; }
//Serialization
[DataMember(IsRequired = true)]
//DataAnnotations
[DisplayName("ChangeDate")]
[Description("Date when product last was changed")]
[Required]
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime ChangeDate { get; set; }
//Serialization
[DataMember]
//DataAnnotations
[DisplayName("Name")]
[Description("Name")]
[Required(ErrorMessage = " ")]
[StringLength(100)]
public String Name { get; set; }
public HttpResponseMessage Post(Entity.Product product,Boolean directSave = true)
{
if (ModelState.IsValid)
{
product.ChangeDate = DateTime.Now;
_productRepository.Add(product);
if (directSave)
_context.Save();
var response = Request.CreateResponse(HttpStatusCode.Created, product);
response.Headers.Location = GetLocation(product.ProductId);
return response;
}
return Request.CreateResponse(HttpStatusCode.BadRequest);
}