Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7176607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:32:40+00:00 2026-05-28T16:32:40+00:00

so I was going on creating my MVC 3 web app when it dawned

  • 0

so I was going on creating my MVC 3 web app when it dawned on me that I might be putting entirely too much logic in my Controller, that it needs to be in the Model instead. The problem with this is, that in this particular instance I’m dealing with a file.

The SQL database table stores the path of the file, and the file itself is saved in a directory. So in the database, the file path is stored as an nvarchar, and in the model, the file is a string, everything’s consistent to that point. The issue comes when it’s time to upload the file, at that point I’m dealing with a System.IO.File.

So the question is, how do you provide System.IO.File logic inside the model for the file when in the back-end it is actually a string?

I had finished the functional version of the Controller and had some logic already in it, and was about to add more when I realized that I was working against the system. What I mean is that in order to have server-side validation, the logic needs to be in the Model in order for the input validation to behave and work according to proper MVC rules, obviously optionally using client-side validation in conjunction.

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 (just the relevant 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 (just the part relevant to the file):

    public string ProfilePhotoPath { get; set; }

So I guess, what are your guys’ solutions in these particular situations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T16:32:41+00:00Added an answer on May 28, 2026 at 4:32 pm

    Description

    Assuming i understand your question. I have read your question a few times. 😉 If i don’t understand, please comment my answer in order to get a better answer (i will update)

    I think that you want is.. How to Model Validation for your particular case.

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

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

    Sample

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

    This will affect ModelState.IsValid

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

    More Information

    • MSDN – ModelStateDictionary.AddModelError Method
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to be creating a web service that will be passing confidential
When creating an index over a column that is going to be UNIQUE (but
I am creating a department picker form that is going to serve as a
I am creating a buffered image that is going to be a snapshot of
Hello I am creating a Windows application (WPF) that is going to be running
I am going to create a new web application that is very customized. It
I like HAML . So much, in fact, that in my first Rails app,
I am creating a Web application using ASP.NET MVC, and I'm trying to use
I'm creating a new ASP.NET MVC application. So far I've used the Account controller
I'm creating a package that is going to be used by R (the statistical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.