我是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();
}
調試,告訴我們哪裏是NRE將是很好;)順便說一下,你最後的foreach當然不會做你想做的事情。 –
謝謝@RaphaëlAlthaus,我不確定這是否正確,所以我在這裏試圖從數據庫中獲取數據,我需要在我的視圖中獲取數據嗎? – 62071072SP
這只是在每個循環中替換'ViewData [「ManufacturerCountryProducts」]'的內容。不知道這是想要的。使用列表,addRange結果(按國家),並將列表(或數組)分配給ViewData出foreach循環會更好。 –