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")]
    public string Description { get; set; }

    [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" })) {

<div class="editor-lable">Image</div>
<div class="editor-=field">
    @if (Model.ImageData == null)
        <img width="150" height="150" src="@Url.Action("GetImage", "Product", new { Model.ProductID })" />
    <div>Upload new image: <input type="file" name="Image" . /></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?


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

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.

In my case the cause of this problem was that assembly containing model class and main web application project had references to different versions of System.Web.Mvc assembly.

If you have couple projects referencing System.Web.Mvc assembly - ensure that used version is the same in all projects.

I had the same problem.

It turned out I had version of System.Web.Mvc in the project that contained the model and of it in the web application project.

To install the correct version you need to run the following in NuGet package mananger:

install-package Microsoft.Aspnet.Mvc ProjectName -version X

replacing ProjectName with the name of your project and X with the version you need to install.

For example:

install-package Microsoft.Aspnet.Mvc TestProject.Web -version

If you omit the version number, NuGet will simply download and install the latest version.

Once I had done this, I also needed to update my unit test project to be on the same version as the web application project. You may need to do the same.

If you use scaffolding the generator will set the input tag with the type hidden in your view. This depends on the T4 Template.

If you create the view manually you must set the field manually. e.g

@Html.HiddenFor(model => model.Id)

I too had this issue. The issue was occurring due to different versions of System.Web.Mvc in different projects of the same solution. I removed and added the references again so that it is same for all the projects(

Need Your Help

Javascript return var

javascript var

The code below is returning gracz.N0P but I want it to return 1, 3, 0 etc.

Video from images array not playing

ios iphone xcode video avassetwriter

Using below code to create video from array of images and saving it in document directory.