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; }
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With