Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:06:24+00:00 2026-06-07T16:06:24+00:00

I am using Asp.Net Mvc Web api RC. I wanted to hide the fields/properties

  • 0

I am using Asp.Net Mvc Web api RC.

I wanted to hide the fields/properties of my model class using custom attribute. Below is my class:

public class Employee
{              
     public int EmpId { get; set; }       
     public string Name{ get; set; }

     //Wanted to hide this attribute based on custom logic. Like for a certain role, i want to hide the designation
     public string Designation{ get; set; }

     public string Department{ get; set; }
}

How can we achieve using Data Annotations. I mean i wanted to create a separate attribute to use in this manner:

[HideForRoles(Roles="Admin,Writer")]
public string Designation{ get; set; }

UPDATE :

As i am developing web api. The response is serialized to either XML or Json format depend upon the formatter. So better question would be how not to allow the fields to be serialize while writing to the response.

However one option could be using IgnoreDataMember attribute. Like

[IgnoreDataMember]
public string Designation{ get; set; }

But the above is a compile time declaration where i cannot impose any condition.

Question: How to ignore the field/property while serializing based on some condition at runtime?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:06:25+00:00Added an answer on June 7, 2026 at 4:06 pm

    I have done the authorization checking in the model repository itself. Rather ideal way was to create custom formatters for hiding the certain fields based on some condition.

    After getting the list of Employees from db and have them in list, i iterated again and place a NULL to the fields i don’t want to display.
    The code i have written as:

    foreach (var employee in listEmployees)
    {
         //get all props. of Employees object using reflection
         var props = employee .GetType().GetProperties();
    
         //loop through each field to match with the field name to remove/place null
         foreach (var propertyInfo in props)
         {
              var fieldName = propertyInfo.Name;
              if (fieldsNamesToRemove .Contains(fieldName))
              {
                        propertyInfo.SetValue(employee , null, null);
              }
         }
     }
    

    here fieldsNamesToRemove is a list that i created dynamically based on roles of current user.

    This solution actually placing a NULL for the fields we do not want display. As a result in JSon format the fields are not displaying but in the XML the fields are displaying with syntax like lt; Designation i:nil=”true”/ gt;, but manageable as we need to deal mostly with json response.

    Thanks Ali and MisterJames for your valuable suggestions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a web API using the REST for ASP.NET MVC framework (MVC 2).
I am using ASP.NET MVC 4 Beta with Web API - although I believe
I am using ASP.NET MVC for developing a web site. I am using jquery
I'm using ASP.NET MVC to develop a web application, deploying to IIS 7. I've
I'm using ASP.net MVC C# in Visual Studio Web Dev. I have a couple
I am building a web application using ASP.NET MVC that has two very distinct
I'm developing a web application using ASP .NET MVC 1.0 and jQuery (including the
I need to create a web application using ASP.NET MVC, jQuery and web standards
I am building a web site in C# using asp.NET MVC How can I
What are the licences required for an asp.net (MVC) web application using sql server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.