I'm trying to add a photo to an employee's registry. All other data such as name, cpf, address and etc are registered normal, but the photo is not, I'm simply taking the name of the photo and saving the way in the bank as I did in other projects, but this time using web api does not work.
My controller:
[HttpPost]
public ActionResult AddOrEdit(EmployeeViewModel employee)
{
if (employee.Id == 0)
{
ViewBag.OcupationId = new SelectList(db.Occupation.ToList(), "Id", "Name");
if (employee.Fotos != null)
{
var pic = Ultilidades.UploadPhoto(employee.Fotos);
if (pic != null)
{
employee.Picture = string.Format("~/Content/Fotos/{0}",pic);
}
}
HttpResponseMessage response = GlobalVariables.WebApiClient.PostAsJsonAsync("Employee",employee).Result;
TempData["SuccessMessage"] = "Empregado cadastrado com sucesso!";
}
My bank class:
public class EmployeeViewModel
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Name { get; set; }
[Required]
[StringLength(50)]
public string CPF { get; set; }
[StringLength(50)]
public string City { get; set; }
[Required]
[StringLength(50)]
public string Adress { get; set; }
public int OcupationId { get; set; }
[Required]
[StringLength(100)]
public string Picture { get; set; }
public virtual Occupation Occupation { get; set; }
public byte[] PicData { get; set; }
public HttpPostedFileBase Fotos { get; set; }
}
My API:
[ResponseType(typeof(Employee))]
public void AddEmployee(Employee employee)
{
if (employee != null)
{
SqlEmployee.CreateEmployee(employee);
}
}
My SQL:
public static void CreateEmployee(Employee employee)
{
connection.Open();//inicia conexao
string select = string.Format("INSERT INTO Employee (Name,CPF,City,Adress,OcupationId,Picture)" +
" VALUES ('{0}','{1}','{2}','{3}','{4}','{5}')", employee.Name, employee.CPF, employee.City,employee.Adress, employee.OcupationId, employee.Picture);
SqlCommand command = new SqlCommand(select, connection);
command.ExecuteNonQuery();
connection.Close();
}
Result: