Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net [HiddenInput] Data Attribute not working when rendered with Html.EditorForModel in Razor?

I have the following model:

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; }
}

I am referencing System.Web.Mvc and 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")

}

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.

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

like image 881
Joshua Andrews Avatar asked Jun 15 '12 14:06

Joshua Andrews


2 Answers

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

like image 133
Achilles Avatar answered Oct 19 '22 08:10

Achilles


I had a similar problem, in my case the problem was caused because of the System.Web.Mvc reference.

I was creating a MVC 3 application, but instead of adding the version 3 of the System.Web.Mvc I added the version 4.

like image 37
J.J Avatar answered Oct 19 '22 07:10

J.J