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 8531171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:25:21+00:00 2026-06-11T09:25:21+00:00

I am basing my solution on this article; http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-and-File-Uploads.aspx However when I try to

  • 0

I am basing my solution on this article;
http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-and-File-Uploads.aspx

However when I try to upload a picture I get a null instead of a filename.

My view looks like this;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SHP.Models.HrViewModel>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit Employee
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <fieldset>
    <legend>Add details to the selected employee</legend>
    <p>The photo you select for an employee will appear on MNet.</p>
    <p>The qualifications you add for an employee will appear on their business cards when required.</p>
        <% using (Html.BeginForm("EditEmployee", "HumanResources", FormMethod.Post,
         new{enctype = "multipart/form-data"}))
           {%>
        <%: Html.AntiForgeryToken() %>
        <%: Html.ValidationSummary(true) %>
        <%: Html.EditorFor(model => model.EmployeeSelector) %>
        <% if (Model.SelectedEmployee != null)
           { %>
                <%: Html.HiddenFor(model => model.SelectedEmployee.EmployeeId) %>
                <%: Html.HiddenFor(model => model.EmployeeName) %>
                <table class="groupBorder" style="margin-top:15px; width:617px;">
                <tbody>
                <tr>
                    <th colspan="2">Add Details for <%: Model.EmployeeName %></th>
                </tr>
                <tr>
                    <td style="text-align: right;">
                <%: Html.LabelFor(model => model.SelectedEmployee.Photo)%>
                   </td>                    
                    <td>
                        <input type="file" id="Picture" name="Picture" />
                    </td>
                </tr>
                <tr>
                    <td style="text-align: right;">
                <%: Html.LabelFor(model => model.SelectedEmployee.Qualifications)%>
                   </td>                    
                    <td>
                <%: Html.TextBoxFor(model => model.SelectedEmployee.Qualifications, new {style = "width:500px;"})%>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align: center;padding-top:20px;">
                    <input type="submit" value="Save" id="btnSubmit" /></td>
                </tr>
                </table>
       <% } %>
        <% } %>
        </fieldset>
</asp:Content>

When you click on the Save button you go to this controller action;

    [HttpPost]
    [Authorize(Roles = "Administrator, HumanResources, ManagerAccounts, ManagerIT")]
    [ValidateAntiForgeryToken]
    [ValidateOnlyIncomingValues]
    public ActionResult EditEmployee(HrViewModel hrvm)
    {
        if (ModelState.IsValid)
        {
            if (hrvm.SelectedEmployee == null
                || hrvm.EmployeeSelector.SearchTextId != hrvm.SelectedEmployee.EmployeeId)
            {
                return this.RedirectToAction(
                    "EditEmployee", new { employeeId = hrvm.EmployeeSelector.SearchTextId });
            }

            if (hrvm.SelectedEmployee.Picture.HasFile())
            {
                var destinationFolder = Server.MapPath("/Users");
                var postedFile = hrvm.SelectedEmployee.Picture;
                var fileName = Path.GetFileName(postedFile.FileName);
                var path = Path.Combine(destinationFolder, fileName);
                postedFile.SaveAs(path);
                hrvm.SelectedEmployee.Photo = path;
            }   

            var emp = Employee.GetEmployee(hrvm.SelectedEmployee.EmployeeId);
            this.TryUpdateModel<IEmployeeHrBindable>(emp, "SelectedEmployee");
            emp.Update();
            this.TempData["Message"] = string.Format(
                "At {0} Details updated for {1}", DateTime.Now.ToString("T"), hrvm.EmployeeName);
            return this.View(hrvm);
        }

        return this.View(new HrViewModel());
    }

So what am I doing wrong?

  • 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-06-11T09:25:23+00:00Added an answer on June 11, 2026 at 9:25 am

    By default, MVC3 performs model binding based on the Name attribute of the input elements in your view.

    To get file upload data, use the HttpPostedFileBase class as a parameter to your ActionResult and call the parameter ‘file’.

    [HttpPost]
    [Authorize(Roles = "Administrator, HumanResources, ManagerAccounts, ManagerIT")]
    [ValidateAntiForgeryToken]
    [ValidateOnlyIncomingValues]
    public ActionResult EditEmployee(HrViewModel hrvm, HttpPostedFileBase file)
    {
        if (ModelState.IsValid)
        {
            if (hrvm.SelectedEmployee == null
                || hrvm.EmployeeSelector.SearchTextId != hrvm.SelectedEmployee.EmployeeId)
            {
                return this.RedirectToAction(
                    "EditEmployee", new { employeeId = hrvm.EmployeeSelector.SearchTextId });
            }
            if (file.ContentLength > 0)
            {
                hrvm.SelectedEmployee.Picture = file;
                var destinationFolder = Server.MapPath("/Users");
                var postedFile = hrvm.SelectedEmployee.Picture;
                var fileName = Path.GetFileName(postedFile.FileName);
                var path = Path.Combine(destinationFolder, fileName);
                postedFile.SaveAs(path);
                hrvm.SelectedEmployee.Photo = path;
            }   
    
            var emp = Employee.GetEmployee(hrvm.SelectedEmployee.EmployeeId);
            this.TryUpdateModel<IEmployeeHrBindable>(emp, "SelectedEmployee");
            emp.Update();
            this.TempData["Message"] = string.Format(
                "At {0} Details updated for {1}", DateTime.Now.ToString("T"), hrvm.EmployeeName);
            return this.View(hrvm);
        }
    
        return this.View(new HrViewModel());
    }
    

    (So if you could grab the image data using model binding, it would have been located at hrvm.Picture instead of hrvm.SelectedEmployee.Picture)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basing on http://www.subshell.com/en/subshell/blog/article-Publishing-Webservices-with-Java-6-and-Spring-WS100.html I try to run Webservice using SimpleJaxWsServiceExporter on Tomcat 7. localhost:8081/WSTest/TourWS?wsdl
I am trying to implement jquery jqmodal at http://communitychessclub.com/test-me.php and am basing my effort
I have a weird problem... Basing my solution on Decrypting a hardcoded file as
What I'm trying to accomplish is to auto-generate tags/keywords for a file upload, basing
I'm basing my question on this one: rspec mocks: verify expectations in it "should"
I've been basing this code off the Bluetooth Chat example. It worked when it
I am trying to transform XML basing on XSLT in Ruby/Nokogiri. doc = Nokogiri::XML(File.read('some_file.xml'))
I am struggling few hours with this and can't find a solution to the
I just finished installing VS 2012 RC and have started working with the ASP.NET
I am writing a http handler that will load a file (css template) modify

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.