2012-09-03 62 views
0

我是Ajax新手,我試圖用jquery調用ajax調用從數據庫中獲取數據。MVC3中的Ajax請求

我國標籤的列表,我試圖當有用戶操作

這裏是我的代碼示例,在跟它有一個空的系統參考的時刻來顯示數據。

public ActionResult Details(int id, string searchTerm) 
     { 
      // Do we have a search term 
      if (!String.IsNullOrEmpty(searchTerm)) 
      { 
       // decode the searchTerm as we dont want to search for encoded bits 
       searchTerm = Decode(searchTerm); 
      } 
     var a = _mRepository.GetMyId(id); 
     if (a == null) 
     { 
      return View("NotFound"); 
     } 


     // Here we need to get list of countries for the selected manufacturer 

     var c = _mwRepository.Getcountries(id); 

     ViewData["Countries"] = c.ToArray(); 



     // Now we need list of products for the manufacturer countries and this is an Ajax call to database so that it fetches data only when there is a user action 
     foreach (var country in c) 
     { 
      var p = _productRepository.GetAllCurrentProductsForManufacturerCountry(id, 
       country.Id); 

     } 
      if(Request.IsAjaxRequest()) 
      { 
            ViewData["ManufacturerCountryProducts"] = 

            p.ToArray(); 
      } 
+0

調試,告訴我們哪裏是NRE將是很好;)順便說一下,你最後的foreach當然不會做你想做的事情。 –

+0

謝謝@RaphaëlAlthaus,我不確定這是否正確,所以我在這裏試圖從數據庫中獲取數據,我需要在我的視圖中獲取數據嗎? – 62071072SP

+0

這只是在每個循環中替換'ViewData [「ManufacturerCountryProducts」]'的內容。不知道這是想要的。使用列表,addRange結果(按國家),並將列表(或數組)分配給ViewData出foreach循環會更好。 –

回答

2
public class CountryProducts 
{ 
    public Country Country { get; set; } 
    public List<Product> ProductsList { get; set; } 
} 

[HttpPost] 
public ActionResult Details(int id, string searchTerm) 
{ 
    // Do we have a search term 
    if (!String.IsNullOrEmpty(searchTerm)) 
    { 
     // decode the searchTerm as we dont want to search for encoded bits 
     searchTerm = Decode(searchTerm); 
    } 
    var a = _mRepository.GetMyId(id); 
    if (a == null) 
    { 
     return View("NotFound"); 
    } 

    // Here we need to get list of countries for the selected manufacturer 
    List<Country> countriesList = _mwRepository.Getcountries(id); 

    var data = new List<CountryProducts>(); 
    if (countriesList != null) 
    { 
     foreach (var country in countriesList) 
     { 
      List<Product> products = _productRepository.GetAllCurrentProductsForManufacturerCountry(id, 
                            country.Id); 
      data.Add(new CountryProducts() {Country = country, ProductsList = products}); 
     } 
    } 

    if (Request.IsAjaxRequest()) 
    { 
     ViewData["CountryProducts"] = data.ToArray(); 
    } 
} 

但我對AJAX的使用兩種不同的方法,如:

public ActionResult Details(int id) 
{ 
    ... 
    return View(model); 
} 

//for ajax request: 
[HttpGet] 
public JsonResult Details(int id, string searchTerm) 
{ 
    return new JsonResult() 
       { 
        JsonRequestBehavior = JsonRequestBehavior.AllowGet, 
        Data = GetFilteredDetailsData(id, searchTerm) 
       }; 
} 
+0

我正在嘗試在我的View Ajax調用中添加我的製造商ID和國家/地區ID如何才能做到這一點? – 62071072SP

+0

manufacturerId = $(「#manufacturerId」)。val(); searchField =「example」; $阿賈克斯({ 類型: 'GET' \t \t \t,網址: '@ Url.Action( 「詳細信息」, 「ControllerName」)' \t \t \t,數據爲:{ID:manufacturerId,SEARCHTERM:searchField} \t \t \t,成功:函數(RESP){ \t \t \t // onSuccessFuntion \t \t \t} }); – Xordal

+0

我不知道,如何格式化代碼,sry – Xordal