Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my model class

C#
public class Email
{
  [Key]
  public int CampaignId { get; set; } 
  public string Name{get;set;} 
  public bool deleteStatus { get; set; }
}
 
ApiController  action
 
// i am passing Name only not deleteStatus
// so by default value should be true inserted in
// deleteStatus column 
public IHttpActionResult PostEmailInsert(EmailCampaign ecamp)  
{
  if (!ModelState.IsValid)
  {
    return BadRequest(ModelState);
  }

  db.Entry(ecamp).State = EntityState.Added;
  db.SaveChanges();
  return Ok(ecamp);
}
Posted
Updated 2-Dec-15 20:08pm
v3

Do something like this-
C#
public IHttpActionResult PostEmailInsert(EmailCampaign ecamp)
{
    if(ecamp.deleteStatus == null)
    {
        ecamp.deleteStatus = true;
    }

    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    db.Entry(ecamp).State = EntityState.Added;
    db.SaveChanges();
    return Ok(ecamp);
}


-KR
 
Share this answer
 
Is it not possible to have a default constructor for your class?

C#
public class Email
{
    public Email()
    {
        CampaignId = 0;
        Name = "";
        deleteStatus = true;
    }

    [Key]
    public int CampaignId { get; set; } 
    public string Name{get;set;} 
    public bool deleteStatus { get; set; }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900