Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add image property in my model?

I just started learning asp.net MVC. How can I add pictures?

My Person model has:

public class Person
{
    public int ID { get; set; }
    public string FirstName{ get; set; }
    public string LastName{ get; set; }
    public int Age { get; set; }                
}
like image 454
MIle Avatar asked Oct 08 '15 10:10

MIle


1 Answers

Ok if you are using the default web application with mvc this would be one way to do it:

add a property for image url to your model

 public class Person
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public string Image { get; set; }
    }

save it and in Models > Identity Model add DbSet for your model

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        //ADD JUST THIS LINE
        public DbSet<Person> People { get; set; }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

Rebuild your solution and add a controller also thick Generate views and the others(Reference script libraries, Use a layout page)

then in your Index view of the controller find this line

<td>
     DisplayFor(modelItem => item.Image)
</td>

and change it with this

<td>
    <img src="@Url.Content(item.Image)" />
</td>

then when you go to create new person you just post any image url you want in the image input

for example this url http://mywindowshub.com/wp-content/uploads/2013/01/user-account.jpg

and on index it will look like this:

you just resize the image for your needs and you`re done. So i hope this helps you.

EDIT:

If you want to add your images from local folder, say you have a folder name images in Content, when you go to create new person in your image input text, you enter the image url like this: ~/Content/images/ImageName.jpg

like image 59
john Avatar answered Sep 21 '22 18:09

john