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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:50:33+00:00 2026-06-02T05:50:33+00:00

Im trying to store an category object in my db and upload an image

  • 0

Im trying to store an category object in my db and upload an image that i can refer to this category,

The category is stored perfectly but the image is not being uploaded for some reason. When i debug i can see that my application never enters the method that stores the file on the server because my file is “Null”.

model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace SkyLearn.Areas.Categories.Models
{
    public class Category
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Icon { get; set; }
        public string Description { get; set; }
    }

    public class CategoryDBContext : DbContext
    {
        public DbSet<Category> categories { get; set; }
    }
}

controller:

//
// POST: /Categories/Category/Create
[Authorize(Roles = "administrator")]
[HttpPost]
public ActionResult Create(Category category, HttpPostedFileBase Icon)
{
    if (Icon != null && Icon.ContentLength > 0)
    {
        // extract only the filename
        var fileName = Path.GetFileName(Icon.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("../Content/icons/"), fileName);
        Icon.SaveAs(path);
        category.Icon = fileName;
    }

    if (ModelState.IsValid)
    {
        db.categories.Add(category);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    return View(category);
}

view:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<SkyLearn.Areas.Categories.Models.Category>" %>

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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<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()) { %>
<form action="" method="post" enctype="multipart/form-data">
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Category</legend>

        <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.Icon) %>
        </div>
        <div class="editor-field">
            <input type="file" name="icon" id="icon"/>
        </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>
</form>
<% } %>

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

</asp:Content>

anybody who can tell me why it wont enter the method that saves the file id be grateful.

i tried looking through the other answers here on stackoverflow and even though some of them had the same problem as me their solutions didnt solve mine.

i also tried changing the upload size etc in my config.web.

please help me 🙂

  • 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-02T05:50:34+00:00Added an answer on June 2, 2026 at 5:50 am

    I don’t know how browsers would treat a nested form, but there’s no point in using Html.BeginForm() and then writing out a form tag manually. Html.BeginForm() has an overload to output Html attributes:

    Html.BeginForm(action, controller, 
        FormMethod.Post, new { enctype="multipart/form-data" })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to store/retrieve a value that is stored in the Application Settings.
I'm trying to store member function pointers by templates like this: (This is a
I'm trying to store the user's ID in the session using django.contrib.auth.login . But
I'm trying to store a url such as: http://localhost/pro_print/index.php#page=home in a variable, but I
OK so im trying to create a settings class to store certain strings that
I am trying to put my app to the amazon store but I get
I'm trying to write a schema that will validate the following XML: <category type=rifles>
Hey I'm trying to learn some of the recommendation algorithms that's being used in
I'm trying to store things into vector but there're few problems. These are my
Im trying to store a list,collection of data objects in Hbase. For example ,a

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.