ASP.Net [HiddenInput]当Html.EditorForModel剃刀中呈现的数据属性不工作? [英] ASP.Net [HiddenInput] Data Attribute not working when rendered with Html.EditorForModel in Razor?

查看:82
本文介绍了ASP.Net [HiddenInput]当Html.EditorForModel剃刀中呈现的数据属性不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下型号:

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

我然后在我看来,这呈现出如下:

I am then rendering this out in my view as follows:

<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] 字段是不实际躲了起来。 HTML源甚至不具有隐藏属性显示出来。

The problem is that while the [Required] annotations are working properly the [HiddenInput] fields are not actually hiding. The html source doesn't even have the hidden attribute showing up.

为什么没有 Html.EditorForModel 应用 [HiddenInput] 属性,这些属性?任何想法?

Why isn't Html.EditorForModel applying the [HiddenInput] attribute to those properties? Any ideas?

推荐答案

在我的情况下,我不得不写的 [HiddenInput] [ HiddenInput(DisplayValue = FALSE)]

And in my case I had to write the [HiddenInput]as [HiddenInput(DisplayValue=false)]

这篇关于ASP.Net [HiddenInput]当Html.EditorForModel剃刀中呈现的数据属性不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆