Search code examples
entity-frameworkef-database-firstasp.net-mvc-5.1

how to concatenate first name and last name and display as full name in view when EF databasefirst


I want to concatenate first name and last name and display it as fullname in view.i tried in usermeta because when you again generate edmx file it will wont affect but error unrecognised fields,any idea how?

public partial class userdetail
{
    public userdetail()
    {
        this.orderdetails = new HashSet<orderdetail>();
    }

    public string userid { get; set; }
    public string username { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }

    public virtual ICollection<orderdetail> orderdetails { get; set; }
}

I created one more class usermeta for validations.

public class Usemeta
{   
    [Required]
    public string userid { get; set; }
    [Required]
    public string username { get; set; }
    [Required]
    public string firstname { get; set; }
    [Required]
    public string lastname { get; set; }
    //[Required]
    //public string Fullname { get { return string.Concat(firstname + "" + lastname); } }
}

And then I created a partial class.

[MetadataType(typeof(Usemeta))]
public  partial class userdetail
{
}

Solution

  • Just add another property in your View and concatenate FirstName and LastName as a read only property , Somthing like this :

    public string FullName 
    {
        get { return FirstName + " " +LastName ;}
    }