0

A few months back, there was a job position who required a pre interview task assessment. I'm actually new to .NET, so I passed and decided to apply to another job which didn't require .net as much. Long story short. Since then, the idea of solving this problem hunts me and It bothers me that I was able to get really close to the solution and didn't actually finished it.

So, if anybody wants to practice their .net /webapi2/ angular skills here it is: screenshot

I get a weird error (NULL values) when I'm almost finishing this task

My Homecontroller.cs

namespace KLab.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Title = "Home Page";

            return View();
        }
        public JsonResult GetSections()
        {
            var db = new KLabDBEntities();
            var samples = db.Samples.Include("User").Include("Status").ToList();
            return Json(samples, JsonRequestBehavior.AllowGet);
        }
    }
}

Since this is WEb API I've changed the angular code to the following: my App.js

var myApp = angular.module('myApp', []);
myApp.controller('maincontroller', function ($scope, SampleService) {

getSamples();

function getSamples() {
    SampleService.getSamples()
        .success(function (Samples) {
            $scope.Samples = Samples;
        });    
}
});

my service.js

myApp.factory('SampleService', ['$http', function ($http) {

var urlBase = 'http://localhost:35656/api';
var SampleService = {};
SampleService.getSamples = function () {
    return $http.get(urlBase + '/Samples');
};

return SampleService;

}]);

My index.cshtml

<h2>Samples</h2>

<div ng-controller="maincontroller">
<table class="table">
<tr>
    <th>Samples Id </th>
    <th>Code Number</th>
    <th>Date </th>
    <th>User Id </th>
    <th>Status Id </th>
</tr>
<tr ng-repeat="Sample in Samples">
    <td>{{Sample.SampleId}} </td>
    <td>{{Sample.CodeNumber}} </td>
    <td>{{Sample.Date.replace('/Date(','').replace(')/','') | date:"MM/dd/yyyy"}} </td>
    <td>{{Sample.UserId}}</td>
    <td>{{Sample.StatusId}}</td>
</tr>
</table>
</div>

Everything else is default from the WeBAPI2 template from Visual Studio. so my Models are generated by Entity Framework:

When I build i get:

Samples Id  CodeNumber  Date    UserId  StatusId

data           data     data      1         6 

data           data     data      2         4 

My goal is to change those id numbers to the actual data of the tables , they are foreign key values

I get no values in that column and when I call the JSON I get:

[{"SampleId":1,"CodeNumber":129076,"Date":"2015-01-02T00:00:00","UserId":6,"StatusId":3,"Status":null,"User":null},
{"SampleId":2,"CodeNumber":850314,"Date":"2015-06-15T00:00:00","UserId":7,"StatusId":3,"Status":null,"User":null},
{"SampleId":3,"CodeNumber":176033,"Date":"2015-07-31T00:00:00","UserId":7,"StatusId":0,"Status":null,"User":null},
{"SampleId":4,"CodeNumber":129629,"Date":"2015-01-21T00:00:00","UserId":3,"StatusId":0,"Status":null,"User":null},"

which is odd since , Status and user are null

Somebody has suggested that this "joins" must be done within a view on the Database itself and then should be called with angular. like a SQL view or using LINQ. Unfortunately, I do not know LINQ but I do know SQL I guess I can create a view with all the tables. Therefore the question is. How can i replace those ID values to the actual data that are related to those foreign keys (userid, statusid) ?

Here's the bulk data's screenshot:

bulk data

brohymn
  • 460
  • 5
  • 22
  • http://stackoverflow.com/questions/4596371/what-are-the-downsides-to-turning-off-proxycreationenabled-for-ctp5-of-ef-code-f , could be because of this – Rohith Nair Jun 25 '16 at 21:05
  • @RohithNair the thing is if i dont turn it off, i dont get data at all i get a 404 error if im not mistaken – brohymn Jun 25 '16 at 21:08
  • Then I think you can include the child entities using include? – Rohith Nair Jun 25 '16 at 21:09
  • @RohithNair How do you that ?...could you explain ? – brohymn Jun 25 '16 at 21:11
  • context.Samples.Include("Status ").Include("Users ") , in the web api get method .Sorry i am not in VS to try this out. – Rohith Nair Jun 25 '16 at 21:16
  • @RohithNair I forgot to post my homecontroller.cs crap!...I'll post it as an update but I already have the line of code you suggested there which was var samples = db.Samples.Include("User").Include("Status").ToList(); – brohymn Jun 25 '16 at 22:11
  • @RohithNair Also if I take off the proxy thing I ge this error message: System.InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Data.Entity.DynamicProxies.Sample_D – brohymn Jun 25 '16 at 22:23
  • Surprised about the downvote? I'm not. You pour out a wall of code and finally end up asking "but then what?...how do i call it on my index.cshtml?...is this the reason why I'm getting NULL values ?" --- What is "it"? NULL values--where? And somewhere in-between there is some doubt about ID values. Is that another question? Make up your mind and ask one pointed question with the appropriate amount of code. – Gert Arnold Jun 25 '16 at 23:36
  • @GertArnold ok. Well i was just showing my steps to get to the point where I am now. At the same time, the question got upvoted too so there's that. Let me edit this question and make it shorter then. – brohymn Jun 26 '16 at 00:49
  • So your *only* question is: I have this query `db.Samples.Include("User").Include("Status").ToList();` but in the end result `Status` and `User` are null. – Gert Arnold Jun 26 '16 at 21:33
  • @GertArnold Pretty much. I just dont get why I'm getting null values. I followed lots of tutorials on webapi/angular but none of them show tables with foreign keys. If I only use 1 table , i wouldnt have this isuue at all – brohymn Jun 26 '16 at 21:37

0 Answers0