Search code examples
asp.net-mvcasp.net-mvc-4html-helper

How to hide primary key input field using editorfor


Hi I'm using Editorfor() to make a little form that submits view model to the controller. Editorfor() nicely prints input fields of the model but it also prints primary key field. So I want to hide primary key field.

@Html.EditorFor(m=>m.viewmodel)

this is markup that I have.

@Html.HiddenFor(m => m.viewmodel.Id);
@Html.EditorFor(m=>m.viewmodel)

have tried this but does not work. and I wanted to make an approach directly to the model but I'm using EF Designer, so I'm not sure where to begin. Please give me an advice.


Solution

  • Try this:

    [HiddenInput(DisplayValue = false)]
    public int ProductID { get; set;