ScaffoldColumn attribute on type object

Deadly 提交于 2019-12-23 01:30:24

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/3575461/scaffoldcolumn-attribute-on-type-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!