c# - 在 ASP.NET 中从视图到控制器的图像为空
问题描述
我有一个小型 ASP NET Web 应用程序,它从视图中获取描述和图像,当我添加描述和图像时,我单击提交按钮,描述很好,但图像始终为空,我不知道为什么。这是我的看法:
@model List<aspnet_client.Models.DataModel>
@{
ViewBag.Title = "AddData";
}
<h2>Añade nuevos registros</h2>
@using (Html.BeginForm("AddData", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true, "", new { @class="text-danger"})
<div class="form-inline">
@if (Model != null && Model.Count > 0)
{
var iterator = 0;
foreach (var i in Model)
{
<div class="data-element">
<div class="description">
@Html.TextBoxFor(x => x[iterator].Description, new { @class = "form-control", placeholder = "Descripción" })
@Html.ValidationMessageFor(model => model[iterator].Description, "", new { @class="text-danger"})
</div>
<br />
<div>
<input type="file" name="Model.Image" id="Image" onchange="fileCheck(this);" />
</div>
</div>
<br />
iterator++;
}
}
</div>
<button type="submit" class="btn btn-success">Añadir Registros</button>
}
如您所见,我在表单中使用了 new { enctype = "multipart/form-data" },所以这不是问题。这是模型:
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace aspnet_client.Models
{
/// <summary>
/// The DataModel class
/// </summary>
public class DataModel
{
/// <summary>
/// Gets or sets the identifier.
/// </summary>
/// <value>
/// The identifier.
/// </value>
public int Id { get; set; }
/// <summary>
/// Gets or sets the description.
/// </summary>
/// <value>
/// The description.
/// </value>
[Required(ErrorMessage = "Se requiere una descripción")]
[DisplayName("Descripción")]
public string Description { get; set; }
/// <summary>
/// Gets or sets the image.
/// </summary>
/// <value>
/// The image.
/// </value>
[Required(ErrorMessage = "Se requiere una imagen")]
[DisplayName("Imagen")]
public byte[] Image { get; set; }
}
}
行动:
using aspnet_client.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace aspnet_client.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult AddData()
{
var records = new List<DataModel>();
// Los registros se añadirán de 5 en 5 basado en los requerimientos de esta prueba
var recordLimit = 5;
for(int a = 0; a < recordLimit; a++)
{
records.Add(new DataModel());
}
return View(records);
}
[HttpPost]
public ActionResult AddData(List<DataModel> records)
{
return View(records);
}
[HttpGet]
public ActionResult GetDataRecords()
{
return View();
}
}
}
图像类型为 byte[] 会不会是个问题?
任何帮助都感激不尽!
解决方案
您可以使用 Request 对象来获取图像,但请确保在控制器中将 img 的 name 属性作为 Image 。
HttpPostedFileBase file = Request.Files["Image"];
推荐阅读
- jquery - 在 nuxt.js 中使用 jquery.easing 时“$.extend 不是函数”
- angular - 排除某些路由的 routerLinkActive 样式类
- flutter - 如何创建可缩放的页面,例如带有颤动的地图
- html - ABCpdf - 如何使用 AddHtml 实现 CSS?
- python - 如何使用自定义 udf 实现来舍入列
- amazon-web-services - AWS rekognition 可以分析视频流中的人脸(情绪等)吗?
- html - 宽度自动和滚动条导致换行
- r - 如何附加超过 2 个文件路径(与 append() 相同)
- python-3.x - 如何在 seaborn 中为气泡图创建尺寸栏?
- ruby-on-rails - 当 MRP 大于 999 时,MRP 价格字段值除以 1000,同时更新 Spree (ROR) 中的值