As we can use System.ComponentModel.DataAnnotations.DisplayAttribute to set a label for a property, I want to use it for the class but it is not allowed on classes.
using System.ComponentModel.DataAnnotations;
[Display(Name = "A person")]
public class Person
{
[Display(Name = "A name")]
public string Name { get; set; }
}
Is anyone know a workaround for that ?
EDIT :
I want to use it on a strongly typed view. When I create a new strongly typed view, the class name is hard coded in HTML, like that :
#model Models.Person
<fieldset>
<legend>Person</legend>
<div class="display-label">
#Html.LabelFor(model => model.Name)
</div>
</fieldset>
I want to do something similar to the Name property.
The DisplayName attribute (from System.ComponentModel) performs a similar function and can be applied to a class.
MSDN
I really don't know if it's there another way to do this, but i usually to not hard code this i use create a variable in the view and then i called where i needed. In your case to do it a little more elegant i'll do
#{
var viewName = typeof(Foo).Name;
}
#model Models.Person
<fieldset>
<legend>#viewName</legend>
<div class="display-label">
#Html.LabelFor(model => model.Name)
</div>
</fieldset>
Using the Decorator Pattern, just wrap the DisplayAttribute with your own custom Attribute specifically for classes.
using System;
using System.ComponentModel.DataAnnotations;
namespace YourNameSpaceHere.Support
{
[AttributeUsage(AttributeTargets.Class)]
public class DisplayForClassAttribute : Attribute
{
protected readonly DisplayAttribute Attribute;
public DisplayForClassAttribute()
{
this.Attribute = new DisplayAttribute();
}
public string ShortName
{
get { return this.Attribute.ShortName; }
set { this.Attribute.ShortName = value; }
}
public string Name
{
get { return this.Attribute.Name; }
set { this.Attribute.Name = value; }
}
public string Description
{
get { return this.Attribute.Description; }
set { this.Attribute.Description = value; }
}
public string Prompt
{
get { return this.Attribute.Prompt; }
set { this.Attribute.Prompt = value; }
}
public string GroupName
{
get { return this.Attribute.GroupName; }
set { this.Attribute.GroupName = value; }
}
public Type ResourceType
{
get { return this.Attribute.ResourceType; }
set { this.Attribute.ResourceType = value; }
}
public bool AutoGenerateField
{
get { return this.Attribute.AutoGenerateField; }
set { this.Attribute.AutoGenerateField = value; }
}
public bool AutoGenerateFilter
{
get { return this.Attribute.AutoGenerateFilter; }
set { this.Attribute.AutoGenerateFilter = value; }
}
public int Order
{
get { return this.Attribute.Order; }
set { this.Attribute.Order = value; }
}
public string GetShortName()
{
return this.Attribute.GetShortName();
}
public string GetName()
{
return this.Attribute.GetName();
}
public string GetDescription()
{
return this.Attribute.GetDescription();
}
public string GetPrompt()
{
return this.Attribute.GetPrompt();
}
public string GetGroupName()
{
return this.Attribute.GetGroupName();
}
public bool? GetAutoGenerateField()
{
return this.Attribute.GetAutoGenerateField();
}
public bool? GetAutoGenerateFilter()
{
return this.Attribute.GetAutoGenerateFilter();
}
public int? GetOrder()
{
return this.Attribute.GetOrder();
}
}
}
Usage would be as follows:
[DisplayForClass(Name = "Approval Matrix")]
public class ApprovalMatrixViewModel
{
}
Related
I want to serialize objects of class to xaml format. However, all of the properties name of class are directly serialized and I can't change their name.
I've used the
[DataMember(Name = "NameToChange")]`
attribute, but this still not solve the problem.
Please help me on this.
Here is the class:
public partial class XObject
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string AtName
{
get
{
return this.Name;
}
set
{
this.Name = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string AtType
{
get
{
return this.m_TypeToken;
}
set
{
this.m_TypeToken = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string AtSerialize
{
get
{
return this.m_SerializeToken;
}
set
{
this.m_SerializeToken = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<string> AtValue
{
get
{
return m_Values;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Dictionary<string, XObject> AtAttached
{
get
{
return m_AttachedAttributes;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string AtDynamic
{
get
{
return m_DynamicValue;
}
}
public void UpdateToken()
{
AtSerialize = (true == HasAttribute(AttributeNameToken_Serialize)) ? GetAttribute(AttributeNameToken_Serialize) : null;
AtType = (true == HasAttribute(AttributeNameToken_Type)) ? GetAttribute(AttributeNameToken_Type) : null;
foreach (XObject member in this)
{
member.UpdateToken();
}
}
private string m_TypeToken = null;
private string m_SerializeToken = null;
}
I want to populate a combobox with two different objects using an interface. This is what I currently got. This works but now I would like to have a display member and value member for each object, how would I do so?
In Controller.cs
public List<IMusic> Populate()
{
List<IMusic> newList = new List<IMusic>();
foreach(Track t in tr.GetAllTracks()){
newList.Add(t);
}
foreach (Artist a in ar.GetAllArtists())
{
newList.Add(a);
}
return newList;
}
IMusic.cs
interface IMusic
{
}
The combobox with DataSource:
cBMainScreen_Search.DataSource = controller.Populate();
GetAllTracks() :
public List<Track> GetAllTracks()
{
return db.Track.ToList();
}
GetAllArtists() :
public List<Artist> GetAllArtists()
{
return db.Artist.ToList();
}
Just setup some properties in your interface:
interface IMusic
{
string Display { get; set; }
string Value { get; set; }
}
Then in your Track class (which should implement IMusic):
public string Display
{
get
{
return this.TrackName;
}
set
{
this.TrackName= value;
}
}
public string Value
{
get
{
return this.TrackID;
}
set
{
this.TrackID= value;
}
}
And in your Artist class (also implements IMusic):
public string Display
{
get
{
return this.ArtistName;
}
set
{
this.ArtistName= value;
}
}
public string Value
{
get
{
return this.AritstID;
}
set
{
this.AritstID= value;
}
}
I am new to Asp .net C#. i have question about objects and inheritance.
if i have parent class (Base-Table) that have 2 child classes (Credit-Card-Table , Bank-Account-Table) i have fun. in another class that take an object from the base-table class.
my problem is i want to know if the Base-table is Credit-card or Bank-account ?!
class BaseTable
{
string date;
public string Date
{
get { return date; }
set { date = value; }
}
string description;
public string Description
{
get { return description; }
set { description = value; }
}
}
class CreditCardTable:BaseTable
{
string Amount;
public string amount
{
get { return Amount; }
set { Amount = value; }
}
string Type;
public string type
{
get { return Type; }
set { Type = value; }
}
}
class BankAccountTable:BaseTable
{
string Refr;
public string Ref
{
get { return Refr; }
set { Refr = value; }
}
string debit;
public string Debit
{
get { return debit; }
set { debit = value; }
}
string credit;
public string Credit
{
get { return credit; }
set { credit = value; }
}
}
3 options:
use is, as or GetType() to explicitly check the type of an instance you have been given, to test it against some known types
if(obj is CreditCardTable) {...} else ...
add a virtual or abstract method to the base-type, and use that instead of ever having to worry about which it is (since it will automatically invoke the most derived override)
obj.SomeMethod();
add a discriminator - perhaps a virtual enum property to the BaseTable which all derived types return a different value from, and switch on that discriminator:
switch(obj.Type) { ... }
I want to add a DisplayAttribute to the Client entity (from another project), but don't want to pollute my entity with attributes specific to MVC or a UI layer. So I planned to add the DisplayAttribute by applying a metadata class to a view model inheriting from the entity
If I inherit from the Client entity and then try to use the MetadataTypeAttribute to add a display attribute, it doesn't show up in the browser. Does anyone know how I can achieve the separation and the functionality of being able to add metadata to my entities?
The Client entity class:
public class Client
{
private string firstName;
private string lastName;
private string homeTelephone;
private string workTelephone;
private string mobileTelephone;
private string emailAddress;
private string notes;
public Title Title { get; set; }
public string FirstName
{
get { return this.firstName ?? string.Empty; }
set { this.firstName = value; }
}
public string LastName
{
get { return this.lastName ?? string.Empty; }
set { this.lastName = value; }
}
public string FullName
{
get
{
List<string> nameParts = new List<string>();
if (this.Title != Title.None)
{
nameParts.Add(this.Title.ToString());
}
if (this.FirstName.Length > 0)
{
nameParts.Add(this.FirstName.ToString());
}
if (this.LastName.Length > 0)
{
nameParts.Add(this.LastName.ToString());
}
return string.Join(" ", nameParts);
}
}
public Address Address { get; set; }
public string HomeTelephone
{
get { return this.homeTelephone ?? string.Empty; }
set { this.homeTelephone = value; }
}
public string WorkTelephone
{
get { return this.workTelephone ?? string.Empty; }
set { this.workTelephone = value; }
}
public string MobileTelephone
{
get { return this.mobileTelephone ?? string.Empty; }
set { this.mobileTelephone = value; }
}
public string EmailAddress
{
get { return this.emailAddress ?? string.Empty; }
set { this.emailAddress = value; }
}
public string Notes
{
get { return this.notes ?? string.Empty; }
set { this.notes = value; }
}
public Client()
{
this.Address = new Address();
}
}
The ClientViewModel view model class:
[MetadataType(typeof(ClientMetaData))]
public class ClientViewModel : Client
{
internal class ClientMetaData
{
[Display(ResourceType = typeof(ResourceStrings), Name = "Client_FirstName_Label")]
public string FirstName { get; set; }
}
}
I think you have change the typeof parameter to:
[MetadataType(typeof(ClientViewModel.ClientMetaData))]
public class ClientViewModel : Client
{
internal class ClientMetaData
{
[Display(ResourceType = typeof(ResourceStrings), Name = "Client_FirstName_Label")]
public string FirstName { get; set; }
}
}
For .Net Core 6.0 use
[ModelMetadataType(typeof(ClientViewModel.ClientMetaData))]
insead of
[MetadataType(typeof(ClientViewModel.ClientMetaData))]
Here are two simple classes to illustrate my question:
class Widget
{
private int _widgetID;
public int WidgetID
{
get { return _widgetID; }
set { _widgetID = value; }
}
private int _categoryID;
public int CategoryID
{
get { return _categoryID; }
set { _categoryID = value; }
}
private string _widgetName;
public string WidgetName
{
get { return _widgetName; }
set { _widgetName = value; }
}
}
And them the second class:
class WidgetCategory
{
private int _widgetCategoryID;
public int WidgetCategoryID
{
get { return _widgetCategoryID; }
set { _widgetCategoryID = value; }
}
private Widget[] _widgets;
public Widget[] Widgets
{
get { return _widgets; }
set { _widgets = value; }
}
private string _widgetCategoryName;
public string WidgetCategoryName
{
get { return _widgetCategoryName; }
set { _widgetCategoryName = value; }
}
}
How would I handle this situation in the most efficient way?
Also, so you know, I will need to nest other classes the same way below the Widget class.
You should create a read-only property of type System.Collections.ObjectModel.Collection<Widget>.
Collection properties should be read only
Use Collection<T>