Title: MVC 3 using the same model in two similar views with non-nullable types
Tags: asp.net-mvc-3;asp.net-mvc-views
Question: I am using the same model on two similar views. One is a users profile view, and the other is the Administrator view for that same page. The administrator's view uses a couple more flags that the user profile view does not have. The issue arises when using non-nullable types. ie. if we do not use a boolean from the model, when the model is posted back to a controller after a "submit", that boolean will default to false and not null, leading to false data if we persist the model.
Apart from having two extremely similar models, what is the MVC 3 proper way of dealing with this?
Thanks!


Here is another answer: As I understand you want to know how can you recognize what the flags were on the form.
Add to your page hidden elements for every checkbox which will indicate about checkbox existence.
Apart from having two extremely similar models, what is the MVC 3
proper way of dealing with this?
You should create partial view.
Regarding to other suggestions, best and SAFE way is in checking (in receive action) what is the flags can current user change (his rights) and than change it in the base.

Here is another answer: You should be aware of the risk of over-posting. It sounds like the admin-view has some way of granting roles or privileges that the profile view doesn't through a set of checkboxes. A smart user could potentially figure out what fields to set to give himself a role or privilege he shouldn't have. If you have one action to handle form data from both views there is no way to tell if this has happened.
The simplest solution (if you do not want to make multiple view models) is to post the form back to different controller actions. The admin-level action will use the booleans to update the model, the other action won't. That prevents any over-posting risks.
Adding hidden fields to the view will give you no security what so ever; do not trust data you get from the browser. Never.

Here is another answer: If I undertand right, you must implemnt nullable bool in your model and act accordingly to handle them.
Look at this answer for a similar rquestion. You will find some links that may be of help
Comment for this answer: In this links only about cast (bool) to (bool?), but not returning NULL from page. Returning NULL from page is imposible physically (because bool is value type). What about (bool?) it is also stucture.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2011:00:09/21/11', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<asp.net-mvc-3><asp.net-mvc-views>', 'Title': 'MVC 3 using the same model in two similar views with non-nullable types', 'ViewCount': 326, 'id': 7495020, 'user_AboutMe': '', 'user_AccountId': 461317.0, 'user_DisplayName': 'abaker', 'user_Reputation': 407.0, 'user_Views': 34.0, 'user_id': 862934.0}}