1

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

3
  • 1
    why are you writing code for httppostedfilebase in model instead do it in post controller???? Commented Aug 1, 2014 at 10:00
  • 1
    The controller is the right place to do any parsing of the HttpRequest. Commented Aug 1, 2014 at 10:02
  • 1
    As a suggestion on the side, Bikes Bicycles = new Bikes(); should be Bikes bikes = new Bikes(); or Bikes _bikes = new Bikes(); otherwise, if you start doing bikes (motorbikes) and bicycles it could get very confusing Commented Aug 1, 2014 at 10:24

1 Answer 1

2

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:

  1. A model class, say, StolenBikeCrime, that represents a stolen bike and any data associated with it.
  2. Another class, StolenBikeRepository, which is responsible for storing and retrieving StolenBikeCrimes 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.