gpt4 book ai didi

c# - 如何在我的模型中添加图像属性?

转载 作者:太空狗 更新时间:2023-10-30 01:17:03 25 4
gpt4 key购买 nike

我刚开始学习 asp.net MVC。如何添加图片?

我的人物模型有:

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

最佳答案

好的,如果您使用带有 mvc 的默认 Web 应用程序,这将是一种方法:

为您的模型添加图片 url 属性

 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; }
}

保存并在 Models > Identity Model 中为您的模型添加 DbSet

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();
}
}

重建你的解决方案并添加一个 Controller 也厚生成 View 和其他(引用脚本库,使用布局页面)

然后在 Controller 的索引 View 中找到这一行

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

然后用这个改变它

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

然后当你去创建新人时,你只需在图像输入中发布你想要的任何图像 url

例如这个网址 http://mywindowshub.com/wp-content/uploads/2013/01/user-account.jpg

在索引上它看起来像这样:

您只需根据需要调整图像大小即可。所以我希望这对你有帮助。

编辑:

如果你想从本地文件夹添加你的图像,假设你在内容中有一个名为图像的文件夹,当你在图像输入文本中创建新人物时,你输入图像 url 如下:~/Content/images/图片名称.jpg

关于c# - 如何在我的模型中添加图像属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33012853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com