Separating MVC Issues

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?

+4
source share
2 answers

Description

Assuming I understand your question. I read your question several times .;) If I do not understand, comment on my answer to get a better answer (I will update)

I think you want ... How to check the model for your specific case.

You can add model validation errors using the ModelState.AddModelError("Key", "Message) method.

ModelState.AddModelError Adds a model error to the error collection for the model dictionary.

Example

 ModelState.AddModelError("ProfilePhotoName", "YourMessage"); 

This will affect ModelState.IsValid

So, you can do whatever you want (your logic), and you can make your model invalid.

Additional Information

+1
source

There are several answers to this question. I hack into it, knowing that the risk could be due to changing opinions. In my personal experience with MVC3, I like to use flatter, simpler Models. If there is a check that can be easily performed in several lines of code that do not require external dependencies, then I will do it in the Model. I don’t feel your System.IO logic is validation as such. The validation that could go into Models, in my opinion, is whether the file name is zero length or not. Saving logic is what you can add to your controller. Even better, you can introduce this logic using the Inversion of Controller pattern and, in particular, the Injection Solution .

0
source

Source: https://habr.com/ru/post/1393560/


All Articles