10

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?

2
  • This should work. I cannot reproduce the problem. Commented Jun 16, 2012 at 10:01
  • i have exactly the same issue Commented Dec 14, 2012 at 12:39

6 Answers 6

9

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

Sign up to request clarification or add additional context in comments.

Comments

6

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.

Comments

2

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.

Comments

2

I had the same problem.

It turned out I had version 5.2.2.0 of System.Web.Mvc in the project that contained the model and 5.2.0.0 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 5.2.2.0

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.

Comments

1

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)

Comments

0

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(4.0.0.1).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.