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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:23:21+00:00 2026-05-20T07:23:21+00:00

i have a view that shows the user a form and the user should

  • 0

i have a view that shows the user a form and the user should upload a file and choose all the categories associated with it.

the controller that is responsible in submitting the data should

  • retrieve the file info and
    insert data in the file category

  • retrieve the related category ids and
    insert them as well in the table
    that is abstracted by the EF
    just
    insert the file and the category ids.

this is my problem the controller just gets some info about the category not all of it. basically it only needs the ids for the insertion

i can’t use

        [HttpPost]
    public ActionResult SaveFile(File file, List<Category> Checkbox, HttpPostedFileBase FileUpload)
    {
        //some stuff
        //for example got the first category and named it to category1
        file.Categories.Add(category1)
    }

i asked someone and he told me you have to select the category you want to insert

is this really necessary ? i only need a category id and a file id to make the insert why would i fire another request to the database that i don’t really need

Entities


i am using

  • EF 4
  • MVC 3
  • 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-20T07:23:22+00:00Added an answer on May 20, 2026 at 7:23 am

    It is better to select category first because it will save you a lot of possible problems but it is not necessary. You can use dummy category object:

      var category = new Category { Id = receivedId };
      file.Categories.Add(category);
    

    You will only create new category and you will set its PK. Now you need to handle file insertion where you must explicitly instruct ObjectContext to insert only file (because your categories exists in database):

    context.Files.Attach(file); // now whole object graph is attached but marked as Unchanged
    context.ObjectStateManager.ChangeObjectState(file, EntityState.Added); // mark only file entity as inserted
    context.SaveChanges();
    

    You can also take opposite direction:

    context.Files.AddObject(file); // all objects in object graph are marked for insertion
    foreach (var category in file.Categories)
    {
      // you don't want to insert categories again
      context.ObjectStateManager.ChangeObjectState(category, EntityState.Unchanged); 
    }
    context.SaveChanges();
    

    This scenario works if you know that all categories exist in your database. If you want to insert new categories together with saving file you will need to query categories first or add some information about which category is new and which is existing.

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

Sidebar

Related Questions

I have a form that shows a project and all the systems associated to
Here is a controller action method that I have to upload a user's profile
I have an iPhone app that shows a simple view (View 1) that has
We have created a new List View Style that shows thumbnails from a picture
I have a UIPopOverController that shows a UIViewController with a UITableview in its view.
I have a user control on a form that can spawn several non-modal child
I have a HomeView with 1 button, that shows the login form. When the
I have a view controller which contains a field for user to enter date
I have a view that has a list of jobs in it, with data
I have a view that I want to add some custom drawing to. I

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.