28

I'm using MVC3. When I make a field with

@Html.EditorFor(model => model.RowKey)

Then the value gets sent back to the controller which is what I need. However I don't want the field to be visible even though I want its value to be returned to the controller.

Is there a way I can make a field hidden with MVC3?

JonathanLaker
  • 303
  • 1
  • 4
  • 5

4 Answers4

52
@Html.HiddenFor(model => model.RowKey)

see What does Html.HiddenFor do? for an example

Community
  • 1
  • 1
moi_meme
  • 8,338
  • 3
  • 44
  • 60
44

If you want to keep Html.EditorFor(), you could always mark a particular property of your model as hidden:

using System.Web.Mvc.HiddenInput;

public class Model
{
    [HiddenInput(DisplayValue = false)]
    // some property
}
Major Productions
  • 5,522
  • 10
  • 63
  • 138
  • I prefer your solution and updated it to include more details about the attribute. And although I don't use the strategy of just doing `Html.EditorForModel()` and prefer to be specific about what to include per property, I just use `Html.EditorFor`. I think if I started again though, I would use `Html.EditorForModel` and customise everything using the scaffolding and attributes. – jamiebarrow Sep 12 '11 at 12:12
  • Is there any fundamental difference between using these two approaches? Except the syntactic sugar perhaps? – Marko Jovanov Nov 25 '15 at 10:38
2

This works for me:

@Html.EditorFor(m => m.RowKey, new { htmlAttributes = new { @style="display: none" } })
Aidan
  • 4,511
  • 2
  • 15
  • 16
1

Could you use the hiddenfor helper, like so:

@Html.HiddenFor(m => m.RowKey)
Robban
  • 6,483
  • 2
  • 32
  • 41