开发者

Ignore some fields using the helper @Html.EditorForModel()

I use helper @Html.EditorForModel() on all my views.

There is a desire that he skip two field开发者_运维技巧s in my model, but only on this view, the other he must continue to display these fields as usual.

How can I skip these two fields only in this view?


Use the [ScaffoldColumn(false)] attribute.

E.g.

public class Person {
    [ScaffoldColumn(false)]
    public int PersonID { get; set; }
    ...

Solution and example sourced from: Pro ASP.NET MVC 3 Framework, Third Edition


I'd recommend writing viewmodels for any view that you want to deviate from default behaviour.

Side note: It's probably a good idea to write a viewmodel for every view, as you get separation of concerns, and it's easier to control the behaviour of each view.

Anyway...

For example, say your model is

class Herps {
    public string Name { get; set; }
    public int SecretToSomePeople { get; set; }
}

and you don't want to have SecretToSomePeople shown on one of your views, create a viewmodel that doesn't contain SecretToSomePeople

class Herps {
    public string Name { get; set; }
}

and use that as the model for the desired view. Make sure you copy to/from the actual model somewhere though.


Strictly speaking, if you don't want to display the fields then they shouldn't be on the Model - the point of Models to to hold exactly the data required for the View.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜