Unable To populate view or view model Updaed

2019-03-03 07:26发布

问题:

Hi I am following up on this question here and working through a proof of concept for a SPA using our Company database data from a series of examples / articles Using Web API 2 with Entity Framework 6

Unfortunately, when I run the project I get this return This feels like a failure to retrieve the data from the SQL Database (SQL Server 2008R2). However I am getting no error message, which I though I would, I know the data view is there as I have checked in SQL Management studio and in Server Explorer of Visual Studio 2017. I am guessing though it could also be an error mapping my Data transfer Objects to my Knockout View Model or my Data bindings to my View.

When I try to look in Visual Studio 2017 I can not find(?) any problems, build errors nor the values of the objects (In the past I have been able to see the local values of objects in vs 2012 build WPF apps).

I could do with someone telling me the best debug steps and where I should be looking in visual studio 2017 to start tracking down these errors, I thought it was the Local tab but this is empty, see screen shot

In summary 1. What is the best way to trap this error 2. Can I find out what has been loaded into my Model and View Modal during run time? 3 If the answer to number 2 is yes, where should I be looking?

Here is my view code

   @section scripts {
    @Scripts.Render("~/bundles/app")
}

<div class="page-header">
    <h1>Requistions Approval</h1>
</div>

<div class="row">

    <div class="col-md-4">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h2 class="panel-title">Requistions</h2>
            </div>
            <div class="panel-body">
                <ul class="list-unstyled" data-bind="foreach: Requistions">
                    <li>
                        <strong><span data-bind="text: Requistion"></span></strong>
                        : <span data-bind="text: Line"></span>
                        : <span data-bind="text: ReqnValue"></span>
                        : <span data-bind="text: OrigName"></span>
                        : <span data-bind="text: LineReqnRaised"></span>
                        : <span data-bind="text: ReasonForReqn"></span>
                        : <span data-bind="text: GLDescription"></span>
                        <small><a href="#">Details</a></small>
                    </li>
                </ul>
            </div>
        </div>
        <div class="alert alert-danger" data-bind="visible: error"><p data-bind="text: error"></p></div>
    </div>

  </div>

Here is my Controller code

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using Requestions_Api_POC.Models;

namespace Requestions_Api_POC.Controllers
{
    public class RequistionsForApprovalsController : ApiController
    {
        private Requestions_Api_POCContext db = new Requestions_Api_POCContext();

        // GET api/RequistionsForApprovals
        public IQueryable<RequistionHeaderDTO> GetRequistionsForApprovals()
        {
            var Requistions = from b in db.RequistionsForApprovals
                              select new RequistionHeaderDTO()
                              {
                                  ID = b.ID,
                                  Requisition = b.Requisition,
                                  ReqnValue = b.ReqnValue,
                                  ApprovedValue = b.ApprovedValue,
                                  OrigName = b.OrigName,
                                  Line = b.Line,
                                  LineReqnRaised = b.LineReqnRaised,
                                  DueDate = b.DueDate,
                                  ReasonForReqn = b.ReasonForReqn,
                                  Supplier = b.Supplier,
                                  GLDesc = b.GLDesc,
                                  CurrentHolder = b.CurrentHolder,
                                  CurName = b.CurName,
                                  CurEmail = b.CurName,
                                  HoldersRouteNum = b.HoldersRouteNum,
                                  DateActioned = b.DateActioned,
                                  DatabaseName = b.DatabaseName,
                                  AdUser = b.AdUser,
                                  ServerName = b.ServerName
                              };
            return Requistions;


        }

Here is my Data Transfer object

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Requestions_Api_POC.Models
{
    public class RequistionHeaderDTO
    {
        public string ID { get; set; }
        public string Requisition { get; set; }
        public decimal? ReqnValue { get; set; }
        public decimal? ApprovedValue { get; set; }
        public string OrigName { get; set; }
        public decimal Line { get; set; }

        public System.DateTime? LineReqnRaised { get; set; }
        public DateTime? DueDate { get; set; }
        public string ReasonForReqn { get; set; }
        public string Supplier { get; set; }
        public string GLDesc { get; set; }
        public string CurrentHolder { get; set; }
        public string CurName { get; set; }
        public string CurEmail { get; set; }
        public decimal? HoldersRouteNum { get; set; }
        public DateTime? DateActioned { get; set; }
        public string DatabaseName { get; set; }
        public string AdUser { get; set; }
        public string ServerName { get; set; }
    }
}

Many thanks

回答1:

To help others.

The problem was as I was adapting the work through to Database first approach and I had not correctly created my model file

To help with debug on the running web app I posted a request to my Get API

api/RequistionsForApprovals

This returned a blank return, proving the problem was with my data reading not my knock out models and bindings. After reading up a bit more I thought I had not defined correctly the primary key of the EF model of my SQL view, therefore I needed to edit the edmx file as in the case here.

However I could not find my edmx file in my solution. Therefore I went through the process of adding a new item, Data, then choosing ADO.net Entity Data model. This created the mappings to my model class and a context which I was able to use with my controller

A bit of a simple mistake, but that is often the case when learning a new process!

Hope it helps anyone else