so I was going to create my MVC 3 web application when it became clear to me that I could use too much logic in my controller to be in the model instead. The problem is that in this particular case I am dealing with a file.
The path to the file is stored in the SQL database table, and the file itself is saved in a directory. Thus, in the database, the path to the file is saved as nvarchar, and in the model, the file is a string compatible with this point. The problem occurs when you need to download a file, at this moment I am dealing with System.IO.File.
So the question is, how do you provide the System.IO.File logic inside the model for the file, if in the background it is actually a string?
I finished the functional version of Controller and already had some logic, and was about to add more when I realized that I was working against the system. I mean, for server-side validation, the logic must be in the Model so that the input verification behaves and works in accordance with the correct MVC rules, obviously, if necessary, using client-side validation.
Currently...
Here is my view:
@model ProDevPortMVC3.Profile @{ ViewBag.Title = "Profile Photo Upload"; } <h2>Photo Upload</h2> <img alt="Profile Image" src="@Html.DisplayFor(model => model.ProfilePhotoPath)" /> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm("UploadPhoto", "Profile", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.ValidationSummary(true) <br /> <input type="file" name="File1" /> @Html.ValidationMessageFor(model => model.ProfilePhotoPath) <input type="submit" value="Upload" /> }
Here is my controller (only appropriate action method):
[HttpPost] public ActionResult UploadPhoto(int id, FormCollection form) { Profile profile = db.Profiles.Find(id); var file = Request.Files[0]; if (file != null && file.ContentLength > 0) { try { string newFile = Path.GetFileName(file.FileName); file.SaveAs(Server.MapPath("/Content/users/" + User.Identity.Name + "/" + newFile)); profile.ProfilePhotoPath = "/Content/users/" + User.Identity.Name + "/" + newFile; UpdateModel(profile); db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } return View(); }
And here is my model (only the part related to the file):
public string ProfilePhotoPath { get; set; }
So, I think, what are the decisions of your guys in these specific situations?