0

Using MVC5 I Have the following defined in the model

   [HiddenInput(DisplayValue = false)]
    public DateTime Created { get; set; }

I do not want this field to appear on any of the MVC generated views. However I am getting a label generated for this hidden field when scaffolding the views, as shown below.

Generated form

What is the correct way to use this attribute so that the field and it's label are not output?

2
  • Do not have any editor templates that may be overriding the default behavior? Commented Mar 27, 2015 at 4:59
  • No... No custom editor templates Commented Mar 27, 2015 at 5:07

4 Answers 4

2
[HiddenInput(DisplayValue = false)]
public int Id { get; set; }

will be rendered as

<input id="Id" name="Id" type="hidden" />

when using Html.EditorForModel() or  Html.EditorFor(m => m.Id)

Check what is render in your UI, type="hidden" or something else.

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

2 Comments

The editor is hidden which is exactly what I want, It's the Label for this hidden input that is the problem
It should be working. You can check with below link : stackoverflow.com/questions/11052293/… stackoverflow.com/questions/2793545/…
0

You can simply use @Html.HiddenFor inside view as shown :-

Model :-

public DateTime Created { get; set; }

View :-

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

2 Comments

But is there no way to get the views that are generated by the scaffolding to be correct, using the data annotation attributes?
@NickLePage...well i think above answer is the correct way to solve your problem ... i personally use this approach..
0

I know this is an old post but I'm experiencing the same thing. My view is set up as

<div class="form-group row">
        @Html.LabelFor(model => model.Summary.LeaID, htmlAttributes: new { @class = "col-form-label col-md-3" })
        <div class="col-md-9">
            @Html.EditorFor(model => model.Summary.LeaID, new { htmlAttributes = new { @class = "form-control"}})
            @Html.ValidationMessageFor(model => model.Summary.LeaID, "", new { @class = "text-danger" })
        </div>
    </div>

And my Model is

[HiddenInput(DisplayValue = false)]
    public string LeaID { get; set; }

When the view renders, I get the label (LabelFor) yet the EditorFor is hidden correctly.

Comments

0

If your only problem is that the label is showing, then annotate the model property with a Display attribute:

[HiddenInput(DisplayValue = false)]
[Display(Name ="")]
public string LeaID { get; set; }

If you do that, the label will not show but still you will have white space on the screen for where the LeaID segment was supposed to appear. Alternatively, you can manually change the view as Kartikeya Khosla has suggested.

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.