Search code examples
asp.net-mvc-2scaffolding

ScaffoldColumn attribute on type object


It seems that to skip a member to come on a View you can set ScaffoldColumn attribute to false in your model

[ScaffoldColumn(false)]
public object Id { get; set; } 

but here i see that Id is of object type. Is this the only way? I tried with

[ScaffoldColumn(false)]
public int Id { get; set; } 

but it didn't work. How can i prevent scaffolding on a primitive type e.g. int,long etc.

Edit

I have define my model as

public class Department
{
[ScaffoldColumn(false)]        
        public int Id { get; set; }

        [Required(ErrorMessage="Name is required")]
        [StringLength(25)]
        [DisplayName("Name")]
        public string Name { get; set; }

        public bool Active { get; set; }
}

I have a controller having action Create. When i right click on create action and select add view and create a strongly type view with this model it creates a view but also adds a textbox for Id

<%: Html.TextBoxFor(model => model.Id)%>

which i suppose it shouldn't have


Solution

  • ScaffoldColumn only changes the behavior of methods like Html.DisplayForModel() which actually use the default templated views system introduced in MVC 2. It does not affect the Visual Studio wizards that ship with MVC.

    To change that, you need to edit the T4 templates, somewhat like this.

    I wouldn't bother, though. If you want scaffolding in MVC 2, I think it's better to use default templated views than the "Add View" scaffolding, which is code generation.