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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:30:17+00:00 2026-06-01T13:30:17+00:00

I’m new to MCV and I’m learning MVC3. I created a model and a

  • 0

I’m new to MCV and I’m learning MVC3. I created a model and a controller and view was generated for me. The generated code makes perfect sense to me. I wanted to modify the generated view and controller so that I could upload a file when I “create” a new record. There is a lot of good information out there about how to do this. Specifically I tried this: http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx

The problem is that even when I select a file (not large) and submit, there are no files in the request. That is, Request.Files.Count is 0.

If I create the controller and and view from scratch, in the same project (no model), the example works just fine. I just can’t add that functionality to the generated page. Basically, I’m trying get the Create action to also send the file. For example, create a new product entry and send the picture with it.

Example Create view:

@model Product.Models.Find

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<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("Create", "Find", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<fieldset>
    <legend>Find</legend>

        <input type="file" id="file" /> 


    <div class="editor-label">
        @Html.LabelFor(model => model.Title)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Title)
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Description)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

Example Controller:

    [HttpPost]
    public ActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            if (Request.Files.Count > 0 && Request.Files[0] != null)
            {
                //Not getting here
            }

            db.Products.Add(product);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(find);
    }

This will create the record just fine but there are not files associated with the Request.

I’ve also tried a controller action like this:

    [HttpPost]
    public ActionResult Create(HttpPostedFileBase file)
    {
        if (file.ContentLength > 0)
        {
            //Not getting here
        }

        return RedirectToAction("Index");
    }

I’m wondering if maybe you can’t post a file at the same time as posting form fields? If that is the case, what are some patterns for creating a new record and associating a picture (or other file) with it?

Thanks

  • 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-01T13:30:18+00:00Added an answer on June 1, 2026 at 1:30 pm

    Create a ViewModel which has properties to handle your image and Product deatils

    public class ProductViewModel
    {
     public string ImageURL { set;get;}
     public string Title { set;get;}
     public string Description { set;get;}
    }
    

    And in your HTTPGET Action method, return this ViewModel object to your strongly typed view

     public ActionResult Create()
     {
            ProductViewModel objVM = new ProductViewModel();
            return View(objVM);
     }
    

    And in your View

    @model ProductViewModel 
    <h2>Add Product</h2>
     @using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
      @Html.TextBoxFor(m => m.Title) <br/>
      @Html.TextBoxFor(m => m.Description ) <br/>
      <input type="file" name="file" />
      <input type="submit" value="Upload"  />
      @Html.HiddenFor(m => m.ImageURL )
    }
    

    Now in your HttpPost action method, accept this ViewModel and File

    [HttpPost]
    public ActionResult Create(HttpPostedFileBase file, ProductViewModel objVM)
    {
      if(file==null)
      {
         return View("Create",objVM);
      }
     else
     {
        //You can check ModeState.IsValid if you have to check any model validations and do further processing with the data here.
        //Now you have everything here in your parameters, you can access those and save
     }        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a jquery bug and I've been looking for hours now, I can't

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.