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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:34:09+00:00 2026-05-27T07:34:09+00:00

I have two models : Category and Picture which refers to two tables, Categories

  • 0

I have two models : Category and Picture which refers to two tables, Categories and Pictures respectively. The Category model has a navigation property to Picture model.

Now, I created a controller using Scaffolding feature with CRUD operations for Category. Following is the code :-

public ActionResult Create()
{
    ViewBag.ParentCategoryId = new SelectList(db.Categories, "Id", "Name");
    ViewBag.PictureId = new SelectList(db.Pictures, "Id", "PictureUrl");
    return View();
}

The automatically generated controller actions uses SelectList for listing the available Picture entries in the database and passes it down to dropdownlist for selection. This is not the ideal scenario since what I want is to unable the user to upload the Picture and then the reference is added to Category model. Later, the entries are saved to Categories and Pictures table.

  • 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-27T07:34:10+00:00Added an answer on May 27, 2026 at 7:34 am

    First of all, I would like to thank @NickLarsen for making me believe that my understanding is good and i can achieve the task myself.

    The problem was not that too tough but since i was new to Asp.net MVC, things were bit baffling. From the very start, I had the notion that i will be needing a ViewModel merging Category and Price classes and then a picture uploading API. But, somehow I wasn’t able to fit the pieces in right place. Therefore after various regression and research over the internet, I achieved the task in following manner :-

    • First of all, I created a ViewModel

      public class CatPicView
      {
          public Category Category { get; set; }
          public Picture Picture { get; set; }
      }
      
    • Second, I added the Uploadify javascript API

      <script type="text/javascript">
          $('#file_upload').uploadify({
              'uploader': '@Url.Content("~/uploadify/uploadify.swf")',
              'script': '@Url.Action("Upload", "Category")',
              'cancelImg': '@Url.Content("~/uploadify/cancel.png")',
              'buttonText': 'Upload',
              'folder': '@Url.Content("~/content/images")',
              'fileDesc': 'Image Files',
              'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
              'auto': true,
              'onComplete': function (event, ID, fileObj, response, data) {
                      var json = jQuery.parseJSON(response);
                      $("#pictureImage").html("<img src='"+json+"' alt='"+json+"' height='100px' width='100px'/>");
                      $("#Picture_PictureUrl").val(json);
                      $("#pictureRemove").show();
                  }
              });
      </script>
      
    • Hooked the API to following Server Function for renaming and saving to folder

      [HttpPost]
      public ActionResult Upload(HttpPostedFileBase fileData)
      {
          if (fileData != null && fileData.ContentLength > 0)
          {
              //var fileName = Server.MapPath("~/Content/Images/" + Path.GetFileName(fileData.FileName));
              int pictureCount = 800000;
              pictureCount += db.Pictures.Count();
              string extension = Path.GetExtension(fileData.FileName);
              string renamedImage = Server.MapPath("~/Content/Images/Categories/cat" + pictureCount + extension);
              fileData.SaveAs(renamedImage);
              return Json("/Content/Images/Categories/" + Path.GetFileName(renamedImage));
          }
          return Json(false);
      }
      
    • And at last, rewrote the Category create Function as below for saving changes to DB

      [HttpPost]
      public ActionResult Create(CatPicView catPic)
      {
          if (ModelState.IsValid)
          {
              if (!String.IsNullOrEmpty(catPic.Picture.PictureUrl))
              {
                  Picture picture = new Picture();
                  picture.PictureUrl = catPic.Picture.PictureUrl;
                  db.Pictures.Add(picture);
                  catPic.Category.PictureId = picture.Id;
              }
              db.Categories.Add(catPic.Category);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View();
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have two models: class Category has many :jobs end class Job belongs_to :category
I have two models, Project and Category, which have a many-to-many relationship between them.
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have two models: store and category with a joining table called categories.stores .
I have two models store and category . A store can have many categories
I have two models Category and Entry . There is another model ExtEntry that
Let's say you have two models: Post and Category . Each Post has a
Say I have two models, Article and Category: class Article(models.Model): category = models.ForeignKey(Category, related_name='articles')
I have two basic models, Story and Category: class Category(models.Model): title = models.CharField(max_length=50) slug
I have a two models: class Category(models.Model): pass class Item(models.Model): cat = models.ForeignKey(Category) 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.