Customer Model
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Group { get; set; }
public string Type { get; set; }
public string Contact { get; set; }
public virtual Address Address { get; set; }
}
Address Model
public class Address
{
public int ID { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string State { get; set; }
public string City { get; set; }
public int CustomerId { get; set; }
}
ViewModel By joining these two
public class ViewModelClient
{
public string Name { get; set; }
public string Group { get; set; }
public string Type { get; set; }
public string Contact { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string State { get; set; }
public string City { get; set; }
}
Controller Method
public ActionResult Index(int id)
{
ViewModelClient Cliensinfo = (from c in db.Customers
join a in db.Addresses on
c.Id equals a.CustomerId
where c.Id == id
select new ViewModelClient
{
Name=c.Name,
Group=c.Group,
Contact=c.Contact,
Type=c.Type,
AddressLine1=a.AddressLine1,
AddressLine2=a.AddressLine2,
State=a.State,
City=a.City
}).FirstOrDefault();
return View(Cliensinfo);
}
And The view
@model WebApplicationCodePro.Models.ViewModelClient
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>