I'm very new to ASP.NET MVC so apologies in advance if this is a stupid question.
I currently have the following code (simplified) in my view:
<form action="/" enctype="multipart/form-data" method="post">
<input type="file" name="files" multiple="">
<button type="submit">Submit</button>
</form>
And I then used the controller to pick up the form data and process it, which worked fine. However, I'm concious that despite it being MVC, I was actually only using the controller and the view parts, not the model.
I've now created the following model, and created placeholder methods as appropiate... in this case I am using the 'Add' method which I've copied the previously working code into:
namespace Stolen_Bikes.Models
{
public class Bikes
{
public int ID { get; set; }
public DateTime DateUploaded { get; set; }
public int staffno { get; set; }
public List<string> files = new List<string>();
public string CrimeRef { get; set; }
public void Add(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
}
}
}
}
I've updated my controller to try to make use of this method, but it's saying I have invalid arguments:
public class HomeController : Controller
{
Bikes Bicycles = new Bikes();
public ActionResult Index()
{
return View();
}
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Index(IEnumerable<HttpPostedFileBase> files)
{
Bicycles.Add(IEnumerable < HttpPostedFileBase > files);
return RedirectToAction("Index");
}
}
Also, despite me defining the datatypes in my Bikes.cs model it doesn't seem like I'm using them anywhere.
For what it's worth, here's the previously working code in my HomeController: http://pastebin.com/mSruqnYW
It's tempting to move my method back into the controller, because it actually worked then - but I'd like to at least try to do things properly, so I'd really appreciate any help with this.
Thanks
Bikes Bicycles = new Bikes();should beBikes bikes = new Bikes();orBikes _bikes = new Bikes();otherwise, if you start doing bikes (motorbikes) and bicycles it could get very confusing