c# - How to use a method from inside a class in ASP.NET MVC? -


I apologize for the ASP.NET MVC long ago if this is a stupid question.

I currently have the following code (simplified):

   

And then I used the controller to take form data and act on it, which works fine. However, I suspect that despite this being MVC, in fact I was only using controller and visible parts, not the model.

I have now made the following model, and made the placeholder method as artificial ... In this case I am using the 'add' method which I copied the code that worked earlier :

  Namespot Stolen_Bikes.Models {PUBLIC Bike Bike {Public Ent ID}; Set; } Public Date Time Date Uploaded {Received; Set; } Public Entitlephone {Received; Set; } Public listing & lt; String & gt; Files = New list & lt; String & gt; (); Public string cryref {receive; Set; } Add Public Zero (IEnumerable & lt; HttpPostedFileBase & gt; Files) {foreach (file in file)} {if (file.ContentLength> gt;) {var fileName = Path.GetFileName (file.FileName); Var path = path Combine (Server MapPath ("~ / App_Data / Upload"), Filename); File.SaveAs (Path); }}}}}  

I have updated my controller to use this method, but it is saying that I have invalid arguments:

  Public Class Home Controller: Controller {Bike Bicycles = New Bike (); Public Performance Index () {Return View (); } Add Public Action Add () {View Return (); } [HTPOST] Public Action Result Index (IINMERABLE & LT; HTTP Posted FileBase & Files; Files) {Bicycles.ed (IINManeble & lt; HTTP Posted FileBase & gt; Files); Return Redirect Action ("Index"); }}  

Also, despite defining the datatpes in my bike CS model, it does not seem that I am using them anywhere.

What is this for me, is the code already working in my Home Controller:

This is temptation to move my method back into the controller, because it actually works - But I want to try to do the least things properly, so I

Invalid argument

in the line

  bicycles. Add (IEnumerable & lt; HTTP posted filebase & gt; files);  

You should not include the type of argument files You just have to pass the files, such as:

  cycl Add (files);  

This is just a matter of C # syntax.

A note on your model

To do this, all your models have to write some posted data on disk. At the same time, it seems that Your model is to represent one or more theft bike. I think that if you divide it into two then you will get a better organization of your code:

  1. A model class, say, StolenBikeCrime , which Represents a stolen bike and data associated with it.
  2. Another class, StolenBikeRepository , which is responsible for storing and retrieving disk (or a database) from /etc/StolenBikeCrime s.

A note on the separation of concerns

Think about your content that translates into the world of HTML and HTML From the Container in your problem domain the controller can rely on other sections to do this task, but your model should not be disturbed by the dirty details of HTTP. As such, you should not go to your add (...) method at HttpPostedFileBase . If you want, pass stream or bytes or any array - that is not related to HTTP.


Comments

Popular posts from this blog

sqlite3 - UPDATE a table from the SELECT of another one -

c# - Showing a SelectedItem's Property -

javascript - Render HTML after each iteration in loop -