【问题标题】:Html - Get value from databaseHtml - 从数据库中获取值
【发布时间】:2017-06-04 02:02:09
【问题描述】:

所以我想在数据库中获取该图像的title。我该怎么做?它为所有图像显示Describe your post...而不是数据库中图像的实际标题

https://i.gyazo.com/17828889116a77983f70fd8c8a4c2ebf.png

查看:

    @foreach (var image in Model.Images)
{
    <h2>@Html.LabelFor(m => m.title)</h2>
    <div class="row">       
        <div class="col-md-8 portfolio-item">
            <img class="portrait" src="@Url.Content(image)" alt="Hejsan" />
        </div>
    </div>

}

型号

 [Table("MemeImages")]
public class UploadFileModel
{
    [Key]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    public int id { get; set; }

    public string location { get; set; }

    public IEnumerable<string> Images { get; set; }

    public int contentLength { get; set; }
    public string contentType { get; set; }

    public string userID { get; set; }

    [Required]
    [Display(Name = "Describe your post...")]
    public string title { get; set; }

    public void SavetoDatabase(UploadFileModel file)
    {
        ApplicationDbContext db = new ApplicationDbContext();                    
        db.uploadedFiles.Add(file);
        db.SaveChanges();
    }
}

【问题讨论】:

    标签: c# html sql


    【解决方案1】:

    而不是标签直接使用&lt;h2&gt;m =&gt; m.title&lt;/h2&gt;

    【讨论】:

    • 这没有意义。
    • 假设您要显示属性的 VALUE 而不是任意标签。好吧,还假设您正确设置了 db 的值
    【解决方案2】:

    移除显示属性。

    [Required]
    public string title { get; set; }
    

    另外,LabelFor() 也不是正确的。您可能希望使用 Display/DisplayFor() 来呈现值。

    【讨论】:

    • 如果我 doi &lt;h2&gt;@Html.DisplayFor(m =&gt; m.title)&lt;/h2&gt; 它不显示任何标题。
    猜你喜欢
    • 2018-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    • 2015-12-13
    • 1970-01-01
    • 2015-08-28
    • 2017-08-29
    • 1970-01-01
    相关资源
    最近更新 更多