Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.EditorForModel and Hiding element from Edit

I'm using the following code to render an editor for my model using ASP.NET MVC 3, it works perfect, except for I don't want the user to see or edit the "Id" field in my object.

<% using (Html.BeginForm())
   { %>
    <%: Html.ValidationSummary(true, "Your input has errors, please correct and try again") %>
    <%: Html.EditorForModel(Model)%>

    <input type="submit" value="Update" />
<% } %>

In my model for the ID Field I have the following

[Display(AutoGenerateField = false)]
public int Id{ get; private set; }

Which granted is what I thought would work based on the description of the "AutoGenerateField" parameter. However this isn't working. I don't want to have to build the whole editor just for this one little oddity....

like image 853
Mitchel Sellers Avatar asked Mar 04 '11 04:03

Mitchel Sellers


2 Answers

Use [ScaffoldColumn(false)] to hide fields

like image 51
Lukáš Novotný Avatar answered Oct 26 '22 17:10

Lukáš Novotný


You could use the [HiddenInput] attribute:

[HiddenInput(DisplayValue = false)]
[Display(AutoGenerateField = false)]
public int Id { get; private set; }
like image 29
Darin Dimitrov Avatar answered Oct 26 '22 18:10

Darin Dimitrov