簡體   English   中英

ASP.NET Core MVC將圖像存儲到數據庫

[英]ASP.NET Core MVC storing image to database

我開始學習ASP.NET Core MVC,並且對將圖像存儲到數據庫有一些疑問。 到目前為止,我的代碼在這里。

詳細模型

using LibaryData.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LibaryManagmentSystems.Models.Catalog
{
    public class AssetDetailModel
    {
        public int AssetID { get; set; }
        public string Title { get; set; }
        public string AuthorOrDirector { get; set; }
        public string Type { get; set; }
        public int Year { get; set; }
        public string ISBN { get; set; }
        public string DeweyCallNumber { get; set; }
        public string  Status { get; set; }
        public decimal Cost { get; set; }
        public string CurrentLocation { get; set; }
        public string ImageURL { get; set; }
        public int PatronName { get; set; }
        public Checkout LatestChechout { get; set; }
        public IEnumerable<CheckoutHistory> CheckoutHistory { get; set; }
        public IEnumerable<AssetHoldModel> CurrentHolds { get; set; }
    }

    public class AssetHoldModel
    {
        public string PatronName { get; set; }
        public string HoldPlace { get; set; }


    }
}

Detail.cshtml

@model LibaryManagmentSystems.Models.Catalog.AssetDetailModel

<div class ="container">
    <div class="page-header clearfix detailHeading"></div>
    <h2 class="text-muted">View Libary Item </h2>
</div>

<div class="jumbotron">
    <div class="row">
        <div class="col-md-4">
            <div>
                <img class="detailImage" src="@Model.ImageURL" />
            </div>
        </div>
        <div class="col-md-4">
            <div>
                <p id="itemTitle">@Model.Title</p>
                <p id="itemAuthor">@Model.AuthorOrDirector</p>
                <p id="itemStatus">@Model.Status</p>
                <p id="itemType">@Model.Type</p>
                <p id="itemLocation">@Model.CurrentLocation</p>

                @if (Model.Status == "Lost")
                  {
            <p>This item has been lost. It cannot be checked out</p>
            <p><a class="btn btn-lg btn-danger" role="button" asp-controller="Catalog" asp-action="MarkFound" asp-route-id="@Model.AssetID">Mark Item Found</a></p>


                 }

                @if (Model.Status == "Checked OUt")
                {
            <p id="itemPatron">Checked Out by: @Model.PatronName</p>
            <p><a class="btn btn-lg btn-success" role="button" asp-controller="Catalog" asp-action="CheckIn" asp-route-id="@Model.AssetID">Check In</a></p>
            <p><a class="btn btn-lg btn-warning" role="button" asp-controller="Catalog" asp-action="Hold" asp-route-id="@Model.AssetID">Place Hold</a></p>


                }

                @if (Model.Status == "Available")
                 {
            <p><a class="btn btn-lg btn-info" role="button" asp-controller="Catalog" asp-action="Checkout" asp-route-id="@Model.AssetID">Check Out</a></p>
                  }

            </div>
            <div class="col-md-4 detailInfo">
                <table>
                    <tr>
                        <td class="itemLabel">ISBN:</td>
                        <td class="itemValue">@Model.ISBN</td>
                    </tr>
                    <tr>
                        <td class="itemLabel">Call Number:</td>
                        <td class="itemValue">@Model.DeweyCallNumber</td>
                    </tr>
                    <tr>
                        <td class="itemLabel">Replacement Cost:</td>
                        <td class="itemValue">@Model.Cost</td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>

然后在數據庫中創建一個fild,將其命名為ImageURL,我將過去的地址保存在文件夾目錄中,並將圖像文件夾保存到C:\\ Desktop \\ LMS \\ images \\ emma.jpg

圖片1

當我運行應用程序時,我得到這樣的圖片

圖片2

到目前為止有什么幫助嗎?

Index.cshtml

@model LibaryManagmentSystems.Models.Catalog.AssetIndexModel


<div id="assets">
    <h3>Libary Catalog</h3>
    <div id="assetsTable">
        <table class="table table-condensed" id="catalogIndexTable">
            <thead>
                <tr>
                    <th>Image</th>
                    <th>Title</th>
                    <th>Author / Director</th>
                    <th>Dew Call Number</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var asset in Model.Assets)
        {
            <tr class="assetRow">
                <td class=""> 
                    <a asp-controller="Catalog" asp-action="Detail" asp-route-id="@asset.Id">
                        <img src="@asset.ImageUrl" class="imageCell" />
                    </a>
                </td>
                <td class="">@asset.Title</td>
                <td class="">@asset.AuthorOrDirector</td>
                <td class="">@asset.DeweyCallNumber</td>
        </tr>
}
            </tbody>
        </table>
</div>
    </div>

問題出在wwwrote文件中的.css文件。 我刪除它並再次創建,它可以工作。 並將圖像插入wwwroot / images目錄中,它將起作用

body {
    padding-top: 50px;
    padding-bottom: 20px;
}

/* Wrapping element */
/* Set some basic padding to keep content from hitting the edges */
.body-content {
    padding-left: 15px;
    padding-right: 15px;
}

/* Carousel */
.carousel-caption p {
    font-size: 20px;
    line-height: 1.4;
}

/* Make .svg files in the carousel display properly in older browsers */
.carousel-inner .item img[src$=".svg"] {
    width: 100%;
}

/* QR code generator */
#qrCode {
    margin: 15px;
}

/* Hide/rearrange for smaller screens */
@media screen and (max-width: 767px) {
    /* Hide captions */
    .carousel-caption {
        display: none;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM