Title: WhiteList subclass via Bind
Tags: asp.net-mvc
Question: Is there a way to white list a subclass via the attribute?
public class VoteQuestionViewModel
[Display(Name = "Vote Question")]
public string Name { get; set; }
public VoteTypeViewModel VoteType { get; set; }
public class VoteTypeViewModel
public int Id { get; set; }
[Display(Name = "Type of Question")]
public string Type { get; set; }
[Display(Name = "Description")]
public string Description { get; set; }
The below will will only work to white list class property, but it will not work to whitelist child class property:
public async Task<IActionResult&gt; Create([Bind("Name, VoteType.Description")] VoteQuestionViewModel voteQuestion)


Here is another answer: The method you included in your question is the only way. Bind works on posted values. In other words, it's looking at the key names in the form data. In that respect, there is no VoteType key and never will be: only the properties on that class that were posted, i.e. VoteType.Description.
That said, not only is it extremely bad practice to use Bind at all, but using it in conjunction with view models makes absolutely no sense. The whole point of Bind is to allow you to include/exclude a subset of properties on an entity class. Likewise, the whole purpose of a view model is to deal with only the data that the view needs to work with. Therefore, if you don't want a property to be included in the post, don't put it on the view model. Even then, view models come with the extra caveat that data has to be mapped to/from the entity class onto to them. That gives you the ability to explicitly decide what will and will not get mapped. For example, if you need Id for some reason in your view, but you don't want the user to be able to change it, simply don't map it from your view model back onto your entity.
Comment for this answer: Good points. At the end I get rid of the Bind feature. I have created additional ViewModel dedicated just for that View, and then used AutoMapper to populate final Model.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:12/19/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net-mvc>', 'Title': 'WhiteList subclass via Bind', 'ViewCount': 52, 'id': 41226364, 'user_AboutMe': '<p>I am a Senior Software Engineer. Most of my work is done in .NET environment writing web and windows application, windows and web services. I like studying and applying IT related concepts, and my main passion is programming and development. I love developing composite solutions combining various technologies. I consider myself a full stack developer, meaning, I develop complete solutions from the database design and implementation till the final product delivery.</p>\n', 'user_AccountId': 3890204.0, 'user_DisplayName': 'lucas', 'user_Reputation': 4157.0, 'user_Views': 127.0, 'user_id': 3220603.0}}