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

  • SEARCH
  • Home
  • 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 7981639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:26:09+00:00 2026-06-04T10:26:09+00:00

I’m having problems getting an uploaded file (HTTPPostedFile) and an object posted to an

  • 0

I’m having problems getting an uploaded file (HTTPPostedFile) and an object posted to an action. I have a class called widget:

public class Widget
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FilePath { get; set; }
}

and in the Widget controller I have an ‘Add’ method

public ActionResult Add()
{
    return View();
}

and an overloaded method to accept what the user posts back

[HttpPost]
public ActionResult Add(Widget widget, HttpPostedFile file)
{
    // Save posted file using a unique
    // Store the path/unique name in Widget.FilePath
    // Save new Widget object
    return View();
}

and in the View I have the following:

@model Project.Models.Widget
@{
    using(Html.BeginForm())
    {
        Html.LabelFor(model => model.FirstName)<br />
        Html.TextBoxFor(model => model.FirstName)<br />
        Html.LabelFor(model => model.LastName)<br />
        Html.TextBoxFor(model => model.LastName)<br />
        <input type="file" id="file" /><br />
        <input type="submit" value="Save" />
    }
}

What I want to do is have the user fill out the form and select a file to upload. Once the file is uploaded, I want to save the file off using a unique name and then store the path of the file as widget.FilePath.

Each time I try, the widget object is populated, but the uploadedFile is null.

Any Help would be greatly appreciated.

  • 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-04T10:26:10+00:00Added an answer on June 4, 2026 at 10:26 am

    There are a couple of issues with your code.

    • Make sure you have set the proper enctype="multipart/form-data" to your form, otherwise you won’t be able to upload any files.
    • Make sure that your file input has a name attribute and that the value of this attribute matches the name of your action argument. Assigning an id has no effect for the server side binding.

    For example:

    @model Project.Models.Widget
    @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.LabelFor(model => model.FirstName)<br />
        @Html.TextBoxFor(model => model.FirstName)<br />
        @Html.LabelFor(model => model.LastName)<br />
        @Html.TextBoxFor(model => model.LastName)<br />
        <input type="file" id="file" name="file" /><br />
        <input type="submit" value="Save" />
    }
    

    Also make sure that your controller action works with a HttpPostedFileBase instead of HttpPostedFile:

    [HttpPost]
    public ActionResult Add(Widget widget, HttpPostedFileBase file)
    {
        // Save posted file using a unique
        // Store the path/unique name in Widget.FilePath
        // Save new Widget object
        return View();
    }
    

    Also you could merge the 2 parameters into a single view model:

    public class Widget
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FilePath { get; set; }
        public HttpPostedFileBase File { get; set; }
    }
    

    and then:

    [HttpPost]
    public ActionResult Add(Widget widget)
    {
        // Save posted file using a unique
        // Store the path/unique name in Widget.FilePath
        // Save new Widget object
        return View();
    }
    

    Finally read the following blog post: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a reasonable size flat file database of text documents mostly saved in
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.