使用 ASP.Net Core 6 创建产品的新实例时无法上传图像

问题描述 投票:0回答:1

Here i tried to write the details of the products and when i clicked the save button it stays the saame我创建了两个模型,一个类别和一个产品。这是每个的代码:

using System.ComponentModel.DataAnnotations;
namespace WebApplication3.Models
{
public class Category
{
public int CategoryId { get; set; }
\[Required\]
\[StringLength(15)\]
public string CategoryName { get; set; } = null!;
}
}


using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;

 namespace WebApplication3.Models
 {
public class Produkt
{
public int Id { get; set; }
public string Name { get; set; } = String.Empty;
public decimal Price { get; set; }
public string? Discription { get; set; }
[Display(Name = "Category Name")]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public string? Image { get; set; } = String.Empty;
[NotMapped]
[DataType(DataType.Upload)]
 [Display(Name = "Upload Image")]
[Required]
public IFormFile? ImageFile { get; set; }
}
}

现在我已经创建了 2 个控制器“带有视图的 MVC 控制器,使用实体框架”,它自动创建了用于创建、删除、详细信息和索引的视图。我的问题是,当我尝试上传图像并单击“保存”按钮时,它不会执行任何操作。 这是创建新产品的视图:

@model WebApplication3.Models.Produkt

@{
ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Produkt</h4>
<hr />
<div class="row">
<div class="col-md-4">
    <form asp-action="Create">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Price" class="control-label"></label>
            <input asp-for="Price" class="form-control" />
            <span asp-validation-for="Price" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Discription" class="control-label"></label>
            <input asp-for="Discription" class="form-control" />
            <span asp-validation-for="Discription" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="CategoryId" class="control-label"></label>
            <select asp-for="CategoryId" class ="form-control" asp-items="ViewBag.CategoryId">           

 </select>
        </div>
        <div class="form-group">
            <label asp-for="Image" class="control-label"></label>
            <input type="file" asp-for="Image" class="form-control" />
            <span asp-validation-for="Image" class="text-danger"></span>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-primary" />
        </div>
    </form>
  </div>
  </div>

 <div>
<a asp-action="Index">Back to List</a>
< /div>

I tried clicking the button but it

我尝试将输入类型更改为图像,但它仍然没有执行任何操作,按钮甚至无法单击。

c# asp.net model-view-controller
1个回答
0
投票

如果要上传图像,则不应使用字符串作为输入。您必须使用 IFormFile。 正如一些朋友提到的,请阅读并学习如何在 stackoverflow 中提问。您没有向我们提供足够的信息,所以这个答案就是我可以给您的。 如果它解决了您的问题,请单击旁边的复选框。 谢谢

© www.soinside.com 2019 - 2024. All rights reserved.