I have a method in web api which i call from the view:
Web api code:
public CarProperties Post(CarProperties car)
{
if(car.file!=null)
{
var path = Path.Combine(Server.MapPath("~/Images/"), Path.GetFileName(car.file.FileName));
car.file.SaveAs(path);
}
new CarRepository().Save(car); //saves the file to database
return car;
}
model:
public class Car{
public int Id {get; set;}
public string Name {get; set;}
public HttpPostedFileBase file {get; set;}
}
View:
@using(Html.BeginForm("Save", "Home", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.TextboxFor(model=>model.file, new { type = "file" })
@Html.TextboxFor(model=>model.Name)
<input type="submit" name="Submit" value="Submit"/>
@Html.ValidationSummary()
}
I get the value of the name in the model but the file is returned as null only.
This same method works if i copy the post method from web api controller to the mvc controller. I cannot understand why it is not working in the web api controller.