2015-05-15 33 views
1

最近我開始在C#中使用ASP.NET MVC進行編程。我遵循教程,但由於某種原因,我的代碼在教程中不起作用。C#MVC - 使用JSON GET請求不加載數據庫字段

我想要做的是做一個同步加載的數據庫表值,而用戶也可以(在等待值加載時)插入值到數據庫中。

加載正在使用JSON GET請求在頁面的html中插入值。

處理輸出列表並返回JSON值的控制器中的ActionResult能否正常工作(通過調試進行測試),但HTML中的JavaScript代碼(GET)永遠不會啓動,因此我從未看到數據在我的HTML數據庫。

當我把一個斷點在HTML中的JavaScript部分,它說以下內容:

的HTML頁面(​​):

The breakpoint will not currently be hit. no executable code of the debugger's target code type is associated with this line.

所有相關的代碼如下所示

@model HelloWorld2.ViewModel.CustomerViewModel 
@using HelloWorld2.Models; 

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>EnterCustomer</title> 

</head> 

<script src="~/Scripts/jquery-1.10.2.js"></script> 
<script src="~/Scripts/jquery.validate.js"></script> 
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script> 

<body> 
    <div>   
     @using (Html.BeginForm("Submit","Customer",FormMethod.Post)) 
     { 

      <i>Customer Name</i> @Html.TextBoxFor(m => m.customer.CustomerName) 
      @Html.ValidationMessageFor(x => x.customer.CustomerName) <br /> 
      <i>Customer Code</i> @Html.TextBoxFor(m => m.customer.CustomerCode) 
      @Html.ValidationMessageFor(x => x.customer.CustomerCode) <br />    

    <input id="Submit1" type="submit" value="submit" /> 

     } 

     <br /> 

     @Html.ValidationSummary() 
     <br /> 

     <div id="status"></div> 
     <table id="tbl"> 
      <tr> 
      <td>Customer Code</td> 
      <td>Customer Name</td> 
      </tr> 
     </table> 
      <script language="javascript"> 

       $("#status").text("Loading...");    
       $.get("getCustomers", null, BindData); 

       function BindData(Customers) 
       { 

        var tbl = $("#tbl"); //reference van table (de id(#) - tbl) 
        //for loop om table te laten zien met alle databaae rows 
        for (var j = 0; j < Customers.lenght ; j++) 
        { 
         var newRow = "<tr>" + 
         "<td>" + Customers[j].CustomerCode + "</td>" + 
         "<td>" + Customers[j].CustomerName + "</td>" + 
         "</tr>"; 
         tbl.append(newRow); //deze row(s) wordt aan de table object verbonden middels append 
        } 
        $("#status").text(""); 
       } 
      </script> 
    </div> 
</body> 
</html> 

The CustomerController

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using HelloWorld2.Models; 
using HelloWorld2.DataAccessLayer; 
using HelloWorld2.ViewModel; 
using System.Threading; 

namespace HelloWorld2.Controllers 
{ 
    public class CustomerController : Controller 
    { 
     // GET: Customer 
     public ActionResult Load() 
     { 
      Customer obj = new Customer {CustomerCode="1001",CustomerName="Nicolas" }; 

      if (Request.QueryString["Type"] == "HTML") 
      { 
       return View("Customer", obj); 
      } 
      else 
      { 
       return Json(obj, JsonRequestBehavior.AllowGet); 
      } 
     } 

     public ActionResult Enter() 
     { 
      CustomerViewModel obj = new CustomerViewModel(); 

      obj.customer = new Customer(); 

      return View("EnterCustomer", obj);    
     } 

     public ActionResult getCustomers()  
     { 
      CustomerDataAccessLayer dal = new CustomerDataAccessLayer(); 
      List<Customer> customerscoll = dal.Customers.ToList<Customer>(); 

      Thread.Sleep(2000); 

      return Json(customerscoll, JsonRequestBehavior.AllowGet); 
     } 

     public ActionResult Submit() 
     { 
      Customer obj = new Customer(); 
      obj.CustomerName = Request.Form["Customer.CustomerName"]; 
      obj.CustomerCode = Request.Form["Customer.CustomerCode"]; 

      CustomerViewModel vm = new CustomerViewModel(); 

      if (ModelState.IsValid) 
      { 
       CustomerDataAccessLayer Dal = new CustomerDataAccessLayer(); 
       Dal.Customers.Add(obj); 
       Dal.SaveChanges(); 

       vm.customer = new Customer(); 
      } 
      else 
      { 
       vm.customer = obj; 
      } 

      List<Customer> customerscollectie = dal.Customers.ToList<Customer>(); 
      vm.customers = customerscollectie; 

      return View("EnterCustomer", vm); 
     } 
    } 
} 

是在控制器代碼被引用到的CustomerDataAccessLayer

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 
using HelloWorld2.Models; 

namespace HelloWorld2.DataAccessLayer 
{ 
    public class CustomerDataAccessLayer : DbContext 
    { 
     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 
      modelBuilder.Entity<Customer>().ToTable("tblCustomer"); 
     } 

     public DbSet<Customer> Customers { get; set; } 
    } 
} 

一些附加分:

  • 已經嘗試過以清除DLL和這樣。
  • ActionResult GetCustomers是與這個問題有關的行爲。
  • 當我把一個調試點放在HTML頁面$("#status").text("Loading...");然後我得到相同的斷點錯誤,雖然文本加載IS顯示在HTML ..(所以它確實到達那裏)。
  • 行動結果GetCustomers正在被觸發,當我加載頁面時,我看到正在加載...對於thread.sleep時間量,當它結束時它只顯示錶頭,而不顯示其中的數據。
  • 我檢查了數據庫表中的連接和數據,它似乎很好,當我調試ActionResult GetCustomers時,那麼列表中也填充了適量的記錄。看來JSON不會返回值..

我希望我的問題很清楚。我只是在ASP.NET MVC初學者和C#爲此事如此過度解釋它絕不會傷害;)

+0

你無法在Visual Studio中調試JavaScript或jQuery代碼。這些是客戶端語言。你將不得不在瀏覽器中進行調試。通過輸入'debugger;'在您要停止的JavaScript行之前/之前。 –

回答

2

When I put a debug point in the HTML page at $("#status").text("Loading..."); then I get the same breakpoint error, though the text loading IS displayed in the html.. (so it does get there).

無法通過把斷點在Visual Studio調試jQuery的。 插件可能是可能的。

但是因爲jQuery,JavaScript是客戶端技術,所以最好在瀏覽器中調試。

所有瀏覽器都有開發工具,您可以通過按F12在Chrome和FireFox中訪問它們。對於FireFox,你可以安裝FireBug

在你想要調試的jQuery行上方輸入'debugger'。

回到瀏覽器。按F12並刷新頁面。

例如,

function BindData(Customers) 
{ 
debugger; 
...... 

} 



function BindData(Customers) 
{ 
// Browser will stop Execution here: 
// In Chrome You can step through code by pressing `F10` 
// And continue by pressing `F8` 

debugger; 

//Display a variable/ string/object in console use console.log('Hello World'); 
// This will display the Customers Object in Console 


console.log(Customers); 

// So by above line you can check if customers are returned from server 

瀏覽器控制檯:

enter image description here

錯誤: 你看不到數據的原因是因爲你在你的代碼有錯誤:

// On this line you have error: 
for (var j = 0; j < Customers.lenght ; j++) 

Should be: 
for (var j = 0; j < Customers.length; j++) 
+0

哇..我怎麼會錯過這個錯字呢..非常感謝!還有關於Javascript的調試信息,不知道Visual Studio無法處理! – Nicolas