How can I return an entity with it's related entities in a Web API method, and call it from a MVC application.
I have a Web API in which I am returning a Person entity, I am also returning all the policies linked to the Person entity.
So a Person can have one or many Policies.
So far I am able to do this in my Web API controller.
The problem is calling my Web API in a separate MVC application.
Currently I can only return the Person record in my MVC application, I am struggling to return the Person and all related Policies in my MVC application.
In my MVC .cshtml view I am calling the ViewModel to pass the data to all the fields in the Web API.
[Route("{pklifeinsured}")]
public IHttpActionResult GetRec(int pklifeinsured)
{
var model = new Amendments_Interface_VM();
using (var db = new TestLiveEntities())
{
var ctx = db.LifeInsureds.Where(l => l.pkLifeInsured == pklifeinsured).FirstOrDefault();
if (ctx != null)
{
model = new Amendments_Interface_VM
{
pkLifeInsured = ctx.pkLifeInsured,
IdNumber = ctx.IdNumber,
Surname = ctx.Surname,
FirstName = ctx.FirstName,
Initials = ctx.Initials,
fkGender = ctx.fkGender,
DateOfBirth = ctx.DateOfBirth,
fkTitle = ctx.fkTitle,
fkMaritalStatus = ctx.fkMaritalStatus,
fkSmokerStatus = ctx.fkSmokerStatus,
fkLanguage = ctx.fkLanguage,
fkCommunicationMethod = ctx.fkCommunicationMethod,
Mobile = ctx.Mobile,
HomeTel = ctx.HomeTel,
EmailHome = ctx.EmailHome,
WorkTel = ctx.WorkTel,
EmailWork = ctx.EmailWork,
Postal_AddressLine1 = ctx.Postal_AddressLine1,
Postal_AddressLine2 = ctx.Postal_AddressLine2,
Postal_AddressLine3 = ctx.Postal_AddressLine3,
Postal_AddressLine4 = ctx.Postal_AddressLine4,
Postal_AddressLine5 = ctx.Postal_AddressLine5,
Physical_AddressLine1 = ctx.Physical_AddressLine1,
Physical_AddressLine2 = ctx.Physical_AddressLine2,
Physical_AddressLine3 = ctx.Physical_AddressLine3,
Physical_AddressLine4 = ctx.Physical_AddressLine4,
Physical_AddressLine5 = ctx.Physical_AddressLine5
};
model.PolicyHolder = db.PolicyHolders.Where(x => x.fkLifeInsured == pklifeinsured).Select(p => new PolicyHolderVM
{
PolicyNumber = p.PolicyNumber,
DOC = p.DOC,
fkLifeInsured = p.fkLifeInsured,
PolicyStatus = p.PolicyStatus,
RelationshipToLifeInsured = p.RelationshipToLifeInsured,
Package = p.Package,
Name = p.Name,
ContactNumber = p.ContactNumber,
ID_Reg_Num = p.ID_Reg_Num,
EmailAddress = p.EmailAddress
}).ToList();
}
}
return Ok(model);
}
public class Amendments_Interface_VM
{
public int pkLifeInsured { get; set; }
public string FirstName { get; set; }
public string Initials { get; set; }
public string Surname { get; set; }
public string fkTitle { get; set; }
public string fkGender { get; set; }
public string IdNumber { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public string fkMaritalStatus { get; set; }
public string fkSmokerStatus { get; set; }
public string fkLanguage { get; set; }
public string fkCommunicationMethod { get; set; }
public string WorkTel { get; set; }
public string Mobile { get; set; }
public string EmailWork { get; set; }
public string HomeTel { get; set; }
public string EmailHome { get; set; }
public string Postal_AddressLine1 { get; set; }
public Nullable<int> PA1_Id { get; set; }
public string Postal_AddressLine2 { get; set; }
public Nullable<int> PA2_Id { get; set; }
public string Postal_AddressLine3 { get; set; }
public Nullable<int> PA3_Id { get; set; }
public string Postal_AddressLine4 { get; set; }
public Nullable<int> PA4_Id { get; set; }
public string Postal_AddressLine5 { get; set; }
public Nullable<int> PA5_Id { get; set; }
public string Physical_AddressLine1 { get; set; }
public Nullable<int> PhyA1_Id { get; set; }
public string Physical_AddressLine2 { get; set; }
public Nullable<int> PhyA2_Id { get; set; }
public string Physical_AddressLine3 { get; set; }
public Nullable<int> PhyA3_Id { get; set; }
public string Physical_AddressLine4 { get; set; }
public Nullable<int> PhyA4_Id { get; set; }
public string Physical_AddressLine5 { get; set; }
public Nullable<int> PhyA5_Id { get; set; }
public IList<PolicyHolderVM> PolicyHolder { get; set; }
}
public class PolicyHolderVM
{
public int PolicyNumber { get; set; }
public Nullable<System.DateTime> DOC { get; set; }
public int fkLifeInsured { get; set; }
public string PolicyStatus { get; set; }
public string RelationshipToLifeInsured { get; set; }
public string Package { get; set; }
public string Name { get; set; }
public string ContactNumber { get; set; }
public string ID_Reg_Num { get; set; }
public string EmailAddress { get; set; }
}
public ActionResult Index(int? pklifeinsured)
{
var LifePolicy = new Amendments_Interface_VM();
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(apiURL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync($"{apiURL}/api/LifePol/{pklifeinsured}").Result;
if (response.IsSuccessStatusCode)
{
LifePolicy = response.Content.ReadAsAsync<Amendments_Interface_VM>().Result;
}
}
return View(LifePolicy);
}
@model WebApiTest_Invoke.Models.Amendments_Interface_VM
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<div class="panel-group" id="MainScreen">
<div class="panel panel-default">
<div class="panel-heading">
<a data-toggle="collapse" data-parent="#MainScreen" href="#collapse1">Policy Holder/s</a>
</div>
</div>
<div id="collapse1" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.PolicyNumber)
</th>
<td>
@Html.DisplayFor(model => Model.PolicyNumber)
</td>
<th>
@Html.DisplayNameFor(model => model.DOC)
</th>
<td>
@Html.DisplayFor(model => Model.DOC)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.fkLifeInsured)
</th>
<td>
@Html.DisplayFor(model => Model.fkLifeInsured)
</td>
<th>
@Html.DisplayNameFor(model => model.PolicyStatus)
</th>
<td>
@Html.DisplayFor(model => Model.PolicyStatus)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.RelationshipToLifeInsured)
</th>
<td>
@Html.DisplayFor(model => Model.RelationshipToLifeInsured)
</td>
<th>
@Html.DisplayNameFor(model => model.Package)
</th>
<td>
@Html.DisplayFor(model => Model.Package)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<td>
@Html.DisplayFor(model => Model.Name)
</td>
<th>
@Html.DisplayNameFor(model => model.ContactNumber)
</th>
<td>
@Html.DisplayFor(model => Model.ContactNumber)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.ID_Reg_Num)
</th>
<td>
@Html.DisplayFor(model => Model.ID_Reg_Num)
</td>
<th>
@Html.DisplayNameFor(model => model.EmailAddress)
</th>
<td>
@Html.DisplayFor(model => Model.EmailAddress)
</td>
</tr>
<tr>
</tr>
</table>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<a data-toggle="collapse" data-parent="#MainScreen" href="#collapse2">Life Insured</a>
<h3 class="panel-title">
@using (Html.BeginForm("Index", "LifePol", FormMethod.Get))
{
<p style="padding-left:700px;">
Policy Number: @Html.TextBox("pklifeinsured")
<input type="submit" value="Search" />
</p>
}
</h3>
</div>
</div>
<div id="collapse2" class="panel-collapse collapse in">
<div class="panel-body">
<table class="table table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.Surname)
</th>
<td>
@Html.DisplayFor(model => Model.Surname)
</td>
<th>
@Html.DisplayNameFor(model => model.fkGender)
</th>
<td>
@Html.DisplayFor(model => Model.fkGender)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
<td>
@Html.DisplayFor(model => Model.FirstName)
</td>
<th>
@Html.DisplayNameFor(model => model.fkSmokerStatus)
</th>
<td>
@Html.DisplayFor(model => Model.fkSmokerStatus)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.fkTitle)
</th>
<td>
@Html.DisplayFor(model => Model.fkTitle)
</td>
<th>
@Html.DisplayNameFor(model => model.DateOfBirth)
</th>
<td>
@Html.DisplayFor(model => Model.DateOfBirth)
</td>
</tr>
<tr>
<th>
@Html.DisplayNameFor(model => model.IdNumber)
</th>
<td>
@Html.DisplayFor(model => Model.IdNumber)
</td>
<th>
@Html.DisplayNameFor(model => model.Mobile)
</th>
<td>
@Html.DisplayFor(model => Model.Mobile)
</td>
</tr>
<tr>
</tr>
</table>
</div>
</div>
</div>
What I have tried:
I have tried using the attached code, but it fails to deserialize the data in my mvc application.project