[RESOLVED]how to add multiple tables using 1 model and 1 view

I want to have a page where the user selects from a drop down list the category, then adds a small text about that category and uploads an image where the path of that image is saved in the database not the whole image. I have created a table categories
where the admin is authorized to fill it. and the user only selects from the categories list. here is what I have done so far:

The create categories model:

using System;

using System.Collections.Generic;

using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace DemoIdentity.Models
public class CategoriesAdmin
public int ID { get; set; }
[Required(AllowEmptyStrings = false)]
[Display(Name = "category name")]
public string categoryName { get; set; }

public class DefaultConnection:DbContext
public DbSet<CategoriesAdmin> categories { get; set; }


Now I want to have another table (Data) which includes (ID,Category(category name selected from table categories), News,Image_Path) this table is in the Default Connection database. the category name is the selected category name from a drop down list, and
the image path is an upload image saves the path only not the whole image.

can anyone please help me with this. I’m new to MVC and still learning.

Thank you in advance

Hi lolo512,

For your requirement, you just need to save the necessary data in another action.

<form action="XX"  method="post" enctype="multipart/form-data">

    <label for="photo">Photo:</label>
    <input type="file" name="photo" id="photo" />
    @Html.DropDownList("Category","your data")
    <input type="submit" value="Upload" />
public ActionResult Upload(HttpPostedFileBase photo,string category)
    string path = @"D:Temp";
    if(photo != null)
        photo.SaveAs(path + photo.FileName);

   //Todo insert into your database

# Uploading and returning files in ASP.NET MVC


Best Regards


how can I get the data from database into a drop down list?


You could read the data as SelectList and set it to ViewBag.

ViewBag.data = new SelectList("your data collection", "Value", "Text", 3);
@Html.DropDownList("name", ViewBag.data as SelectList,"select")


Leave a Reply