EDN Admin
Well-known member
Hi everybody,
I defined the following class (this is EF Code First class):
<pre class="prettyprint using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
namespace CardNumbers.Objects
{
[ComplexType]
public class PhoneInfo
{
[DataType(DataType.PhoneNumber)]
[DisplayName("Phone")]
[RegularExpression(@"^(((d{3})|d{3})s?)?d{3}[-s]?d{4}s*$", ErrorMessage = "Please enter valid Phone Number")]
public virtual string Phone { get; set; }
[StringLength(5)]
[DisplayName("Ext")]
public virtual string Ext { get; set; }
public bool HasValue
{
get
{
return (Phone != null || Ext != null);
}
}
}
[ComplexType]
public class ContactDetail
{
//Constructor
public ContactDetail()
{
phoneInfo = new PhoneInfo();
}
[StringLength(100)]
[DisplayName("Contact Name")]
[DisplayFormat(NullDisplayText = "")]
public virtual string Contact { get; set; }
[StringLength(100)]
[DataType(DataType.EmailAddress)]
[DisplayName("Email")]
public virtual string Email { get; set; }
public virtual PhoneInfo phoneInfo { get; set; }
public bool HasValue
{
get
{
return (Contact != null || Email != null || phoneInfo.HasValue);
}
}
}
/// <summary>
/// Client class (Client No, Client Name, Address, Contact1, Contact2 info, Created By, Modified By (operator and date)
/// </summary>
public class Client
{
public Client()
{
Contact1 = new ContactDetail();
Contact2 = new ContactDetail();
}
[Key]
[Editable(false)]
[Column("ClientId",TypeName = "int")]
public virtual int Id { get; set; }
[Required]
[DisplayName("Client No")]
[Column("client_no", TypeName = "smallint")]
public virtual Int16 Number { get; set; }
[Required]
[Column("client_name", TypeName = "varchar")]
[DisplayName("Client Name")]
[MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters" )]
[MinLength(3, ErrorMessage = "Client Name is too short")]
public virtual string Name { get; set; }
[DataType(DataType.MultilineText)]
public virtual string Address { get; set; }
public virtual ContactDetail Contact1 {get; set;}
public virtual ContactDetail Contact2 {get; set;}
[ForeignKey("EnteredByOperator")]
public string EnteredBy { get; set; }
[InverseProperty("ClientsEnteredBy")]
public virtual Operator EnteredByOperator { get; set; }
[ForeignKey("ModifiedByOperator")]
public string ModifiedBy { get; set; }
[InverseProperty("ClientsUpdatedBy")]
public virtual Operator ModifiedByOperator { get; set; }
[DataType(DataType.DateTime)]
[DisplayName("Created on")]
public DateTime EnteredOn { get; set; }
[DataType(DataType.DateTime)]
[DisplayName("Modified on")]
public DateTime? ModifiedOn { get; set; }
public virtual ICollection<ClientOrder> ClientOrders { get; set; }
public virtual ICollection<Reorder> Reorders { get; set; }
}
}[/code]
<br/>
Now, in my other project I defined the following class:
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CardNumbers.Objects;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
namespace CardNumbers.Models
{
public class ClientViewModel
{
public Client Client { get; set; }
[Required]
[DisplayName("Client No")]
[UIHint("Number")]
[Column("client_no", TypeName = "smallint")]
[Remote("doesClientNoExist", "Client", HttpMethod = "POST", ErrorMessage = "Client Number already exists. Please enter a different Client Number.")]
public virtual Int16 Number { get; set; }
[Required]
[Column("client_name", TypeName = "varchar")]
[DisplayName("Client Name")]
[MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters")]
[MinLength(3, ErrorMessage = "Client Name is too short")]
[Remote("doesClientNameExist", "Client", HttpMethod = "POST", ErrorMessage = "Client Name already exists. Please enter a different Client Name.")]
public virtual string Name { get; set; }
public int id { get; set; }
}
}[/code]
<br/>
I want to be able to have some properties directly of that view model class, but they should automatically link to the Clients properties. E.g. if I populate Name, it should automatically update Client.Name and the original value of the Name property should
be the same as Client.Name. Same goes for the Number property.
I may want to expose a few more properties to use them directly instead of Client. properties.
Also, I am wondering if there is a way to somehow carry on attributes so I dont have to repeat them twice?
So, do you see what I am getting at and can you suggest me a resolution?
Thanks a lot in advance. <hr class="sig <font color="blue For every expert, there is an equal and opposite expert. - Beckers Law
<br/>
<br/>
<br/>
<b> http://blogs.lessthandot.com/index.php/All/?disp=authdir&author=218 My blog </b></font>
[url=http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/87b05171-7ecc-4ac8-9954-e18d188c17d9]View the full article[/url]
I defined the following class (this is EF Code First class):
<pre class="prettyprint using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
using System.Collections.Generic;
namespace CardNumbers.Objects
{
[ComplexType]
public class PhoneInfo
{
[DataType(DataType.PhoneNumber)]
[DisplayName("Phone")]
[RegularExpression(@"^(((d{3})|d{3})s?)?d{3}[-s]?d{4}s*$", ErrorMessage = "Please enter valid Phone Number")]
public virtual string Phone { get; set; }
[StringLength(5)]
[DisplayName("Ext")]
public virtual string Ext { get; set; }
public bool HasValue
{
get
{
return (Phone != null || Ext != null);
}
}
}
[ComplexType]
public class ContactDetail
{
//Constructor
public ContactDetail()
{
phoneInfo = new PhoneInfo();
}
[StringLength(100)]
[DisplayName("Contact Name")]
[DisplayFormat(NullDisplayText = "")]
public virtual string Contact { get; set; }
[StringLength(100)]
[DataType(DataType.EmailAddress)]
[DisplayName("Email")]
public virtual string Email { get; set; }
public virtual PhoneInfo phoneInfo { get; set; }
public bool HasValue
{
get
{
return (Contact != null || Email != null || phoneInfo.HasValue);
}
}
}
/// <summary>
/// Client class (Client No, Client Name, Address, Contact1, Contact2 info, Created By, Modified By (operator and date)
/// </summary>
public class Client
{
public Client()
{
Contact1 = new ContactDetail();
Contact2 = new ContactDetail();
}
[Key]
[Editable(false)]
[Column("ClientId",TypeName = "int")]
public virtual int Id { get; set; }
[Required]
[DisplayName("Client No")]
[Column("client_no", TypeName = "smallint")]
public virtual Int16 Number { get; set; }
[Required]
[Column("client_name", TypeName = "varchar")]
[DisplayName("Client Name")]
[MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters" )]
[MinLength(3, ErrorMessage = "Client Name is too short")]
public virtual string Name { get; set; }
[DataType(DataType.MultilineText)]
public virtual string Address { get; set; }
public virtual ContactDetail Contact1 {get; set;}
public virtual ContactDetail Contact2 {get; set;}
[ForeignKey("EnteredByOperator")]
public string EnteredBy { get; set; }
[InverseProperty("ClientsEnteredBy")]
public virtual Operator EnteredByOperator { get; set; }
[ForeignKey("ModifiedByOperator")]
public string ModifiedBy { get; set; }
[InverseProperty("ClientsUpdatedBy")]
public virtual Operator ModifiedByOperator { get; set; }
[DataType(DataType.DateTime)]
[DisplayName("Created on")]
public DateTime EnteredOn { get; set; }
[DataType(DataType.DateTime)]
[DisplayName("Modified on")]
public DateTime? ModifiedOn { get; set; }
public virtual ICollection<ClientOrder> ClientOrders { get; set; }
public virtual ICollection<Reorder> Reorders { get; set; }
}
}[/code]
<br/>
Now, in my other project I defined the following class:
<pre class="prettyprint using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CardNumbers.Objects;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;
namespace CardNumbers.Models
{
public class ClientViewModel
{
public Client Client { get; set; }
[Required]
[DisplayName("Client No")]
[UIHint("Number")]
[Column("client_no", TypeName = "smallint")]
[Remote("doesClientNoExist", "Client", HttpMethod = "POST", ErrorMessage = "Client Number already exists. Please enter a different Client Number.")]
public virtual Int16 Number { get; set; }
[Required]
[Column("client_name", TypeName = "varchar")]
[DisplayName("Client Name")]
[MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters")]
[MinLength(3, ErrorMessage = "Client Name is too short")]
[Remote("doesClientNameExist", "Client", HttpMethod = "POST", ErrorMessage = "Client Name already exists. Please enter a different Client Name.")]
public virtual string Name { get; set; }
public int id { get; set; }
}
}[/code]
<br/>
I want to be able to have some properties directly of that view model class, but they should automatically link to the Clients properties. E.g. if I populate Name, it should automatically update Client.Name and the original value of the Name property should
be the same as Client.Name. Same goes for the Number property.
I may want to expose a few more properties to use them directly instead of Client. properties.
Also, I am wondering if there is a way to somehow carry on attributes so I dont have to repeat them twice?
So, do you see what I am getting at and can you suggest me a resolution?
Thanks a lot in advance. <hr class="sig <font color="blue For every expert, there is an equal and opposite expert. - Beckers Law
<br/>
<br/>
<br/>
<b> http://blogs.lessthandot.com/index.php/All/?disp=authdir&author=218 My blog </b></font>
[url=http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/87b05171-7ecc-4ac8-9954-e18d188c17d9]View the full article[/url]