【发布时间】:2011-11-15 13:37:10
【问题描述】:
我在 .net 4 中使用 EF + MVC3,我的表单中有一个上传字段,在我的开发服务器上,它可以按预期完美运行,但是当我将它移到我的生产服务器时,无论何时表单已发布我收到The value '' is invalid. 验证错误,即使我提交了图像(我已在 FireBug 中签入,并且该字段已填充)。
在网上看,每当出现这种情况时,似乎都是因为 EF 模型的设置者是非公开的。但事实并非如此,因为模型上的这个属性不会映射到数据库中的任何字段(我不直接使用 EF Poco,我已经围绕它们制作了模型)。
我认为这可能是 IIS 差异,因为我的开发服务器运行的是 IIS7 .net4,而生产服务器运行的是 IIS6 .net4。
有没有人知道为什么会发生这种情况?
型号:
public class AddBookModel {
public AddBookModel () { }
public AddBookModel(Book book) {
Title = book.Title;
Description = HttpUtility.HtmlDecode(book.Description);
CoverExtension = book.CoverExtension;
}
[Required(ErrorMessage="This field cannot be blank")]
[DataType(DataType.Html)]
[Display(Name="Description:")]
public string Description { get; set; }
[Required(ErrorMessage="This field cannot be blank")]
[MaxLength(50)]
[Display(Name="Title:")]
public string Title { get; set; }
private string _CoverExtension = String.Empty;
public string CoverExtension {
get { return _CoverExtension; }
private set { if (value != null) { _CoverExtension = value; } }
}
public virtual string CoverPath { get { return String.Empty; } }
private HttpPostedFileBase _Image;
public HttpPostedFileBase Image {
get { return _Image; }
set { _Image = value; CoverExtension = value.GetFileExtension(); }
}
public ResultStatuses ActionStatus { get; set; }
}
查看(相关位):
<div class="field @if (ViewData.ModelState["Image"] != null && ViewData.ModelState["Image"].Errors.Count > 0) { <text>field-error</text>}">
@Html.LabelFor(m => m.Image)
@Html.Upload("Image")
@Html.ValidationMessageFor(m => m.Image, null, new { @class="error-hint" })
</div>
更新
我现在检查了我工作场所的代码,它运行与生产服务器相同的 IIS 版本(我相信),它工作正常,除了向我尝试上传的文件系统写入权限文件,但这不会与验证错误有关吗?
Html 上传助手:
public static MvcHtmlString Upload(this HtmlHelper helper, string name) {
string result = string.Format("<input type=\"file\" id=\"{0}\" name=\"{0}\" />", name);
return MvcHtmlString.Create(result);
}
【问题讨论】:
-
绝对不知道,尤其是没有代码。 64/32 位你会更早地得到一个错误。
-
没有看到你的代码就没有线索。
-
你的图片没有验证规则,它不应该是无效的。此外,您正在使用一些第 3 方 Html 帮助器作为上传帮助器,所以不知道那是什么......
-
@Mystere Man 没错,这就是为什么我对它为什么会给出验证错误有点困惑。我会把助手的代码放在上面,但它实际上是一个渲染输入的单行代码
-
其他事情正在发生。 MVC 只能在字段上有属性时进行验证。它根本不可能在没有任何属性的字段上验证失败。
标签: entity-framework asp.net-mvc-3 validation poco