我有以下型号:
public class Product { [HiddenInput(DisplayValue = false)] public int ProductID { get; set; } [required(ErrorMessage="Please enter a product name")] public string Name { get; set; } [required(ErrorMessage="Please enter a description")] [DataType(DataType.MultilineText)] public string Description { get; set; } [required] [Range(0.01,double.MaxValue,ErrorMessage="Please enter a positive price")] public decimal Price { get; set; } [required(ErrorMessage="Please specify a category")] public string Category { get; set; } public byte[] ImageData { get; set; } [HiddenInput(DisplayValue = false)] public string ImageMimeType { get; set; } }
我引用了System.Web.Mvc和System.ComponentModel.DataAnnotations.
我在这里提出如下:
<h1>Edit @Model.Name</h1> @using (Html.BeginForm("Edit","Admin",FormMethod.Post,new { enctype = "multipart/form-data" })) { @Html.EditorForModel() <div class="editor-lable">Image</div> <div class="editor-=field"> @if (Model.ImageData == null) { @:None } else { <img width="150" height="150" src="@Url.Action("GetImage","Product",new { Model.ProductID })" /> } <div>Upload new image: <input type="file" name="Image" . /></div> </div> <input type="submit" value="Save" /> @Html.ActionLink("Cancel and return to List","Index")
}
解决方法
在我的情况下,我必须将[HiddenInput]写成[HiddenInput(DisplayValue = false)]