Reputation: 3965
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
Upvotes: 1
Views: 2490
Reputation: 8380
Invalid arguments
In the line
Bicycles.Add(IEnumerable < HttpPostedFileBase > files);
you should not include the type of the files
argument. You should just pass files, like so:
Bicycles.Add(files);
This is just a matter of C# syntax.
A note on your model
All your model is doing is to write some POSTed data to disk. At the same time, it seems your model is meant to represent one or more stolen bikes. I think you would get a better organisation of your code if you split it into two:
StolenBikeCrime
, that represents a stolen bike and any data associated with it.StolenBikeRepository
, which is responsible for storing and retrieving StolenBikeCrime
s to/from disk (or a database). You put most of your current logic in here.A note on separation of concerns
Think of your controller as the thing that translates stuff from the world of HTTP and HTML to your problem domain. The controller can rely on other classes to perform this work, but your model should not be bothered with the nasty details of HTTP. Thus, you should not pass on a HttpPostedFileBase
to your Add(...)
method. If you must, pass on a Stream
or an array of bytes or whatever - something that is not related to HTTP.
Upvotes: 2