2013-11-24 44 views
0

我的下面的代碼我用ajax調用部分視圖,但是當我點擊產品名稱的鏈接時,沒有通過ajax檢索到ajax的錯誤並執行該產品的描述。我正在檢索用戶在同一頁面上選擇的項目的詳細信息,但未找回。請給出任何建議,因爲我是MVC的新手,問題在哪裏出現。謝謝...Ajax不檢索mvc中的部分視圖中的數據

Create.cshtml

@model List<PartialView.Models.tbl_product> 

<!DOCTYPE html> 
<html> 
<head> 
    <title>Create</title> 
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $('.msg').click(function() { 
       var id = this.id; 
       $.ajax({ 
        url: "/Category/Display", 
        data: { data: id }, 
        success: function (mydata) { 
         $("#link").empty().append(mydata); 
        }, 
        error: function (mydata) { alert("error"); }, 
        type: "POST" 
       }); 
       return false; 
      }); 
     }); 
    </script> 
</head> 
<body> 
@foreach (var item in Model) 
{ 
<a class="msg" href="#" id="@item.ProductId">@item.ProductName</a> 
} 
<div id="link"> 
</div> 
</body> 
</html> 

ClicksUs.cshtml(PartialView)

@model List<PartialView.Models.tbl_product> 

@foreach(var items in Model) 
{ 
    @items.ProductDesc 
} 

CategoryController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using PartialView.Models; 

namespace PartialView.Controllers 
{ 
    public class CategoryController : Controller 
    { 
     dbEntities dbentity = new dbEntities(); 

     public ActionResult Create() 
     { 
      return View(dbentity.tbl_product.ToList()); 
     } 

     public ActionResult Display(int data) 
     { 
      var query = dbentity.tbl_product.First(c => c.ProductId == data); 
      return PartialView("ClicksUC", query); 
     } 

    } 

} 
+0

你可以發佈什麼,你從你的ajax調用 –

+0

顯示方法獲取id假設,如果我點擊鏈接的id是2然後顯示方法獲取該id和查詢檢索其ID爲2的項目的詳細信息但是當ajax函數中的控件進入錯誤塊,並且如果我打印警報(mydata),它會打印[object Object],請給出任何建議.. – user3026519

+0

使用json返回部分視圖:return View(「PartialViewName」,jsonData) –

回答

2

Details控制器動作在這裏選擇一個元素(因爲你在呼喚.First()):

public ActionResult Display(int data) 
{ 
    var query = dbentity.tbl_product.First(c => c.ProductId == data); 
    return PartialView("ClicksUC", query); 
} 

所以查詢變量的類型是tbl_product而不是List<tbl_product>

另一方面,您的部分模型是List<PartialView.Models.tbl_product>這顯然是錯誤的。

你部分的模式應該是一個單一的tbl_product

@model PartialView.Models.tbl_product 
@Model.ProductDesc 

哦,關於你的局部視圖名稱錯字說別人。

+0

+1 darin,我正在編輯補充說,但你說第一(關於奇異桌子);) –

0

有代碼的三個問題,你可以解決。

  • 一個是錯字(該partialview稱爲ClicksUS,NOT ClicksUC)
  • 另一個是與你的方式返回數據
  • 第三個就是你用type: "POST",你應該改變這到type: "GET"

嘗試的代碼更改爲:

public ActionResult Display(int data) 
{ 
    // using First() would have caused you an error in the view if not found 
    // still not perfect below, but a step closer 
    var query = dbentity.tbl_product.FirstOrDefault(c => c.ProductId == data); 
    // You had ClicksUC, rather than ClicksUS 
    return PartialView("ClicksUS", query); 
} 

我會還強烈建議您爲您的數據ViewModel,而不是從數據庫中傳遞的對象,因爲這將讓您精確地控制應該看到的數據以及應如何進行格式化等

[編輯] 而且,達林說,基於單排被retruned,你應該改變你的局部視圖模型類型:

@model PartialView.Models.tbl_product 
+0

.First()。ToList()??????你知道.First()方法返回並執行什麼嗎? –

+0

正確:-) - firstordefault是我的意思 - 星期天早上maddness ..原諒! –