제품 데이터가로드 된보기가 있습니다. I 버튼 '바구니에 추가'를 누르면 다시 다시로드 같은 페이지를하고 싶습니다하지만 난 같은 오류를 받고 있어요 :새로 고침 오류시 데이터가있는보기
Object reference not set to an instance of an object.
보기 :
@model List<Ecommerce.Models.HomeModels.Product>
@foreach (Ecommerce.Models.HomeModels.Product product in Model)
{ // above error points here!!!!!!!!!!!
using (Html.BeginForm())
{
<input type="hidden" name="productId" value="@product.ID" />
<input type="submit" value="Add to Basket"/>
}
}
컨트롤러 :
public ActionResult BuyProducts()
{
List<Models.HomeModels.Product> products = new List<Models.HomeModels.Product>();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
//Method to load data into products
}
}
TempData["products"] = products;
return View(products);
}
[HttpPost]
[AllowAnonymous]
public ActionResult BuyProducts(string productID)
{
string id = productID;
return View(TempData["products"]);
}
고마워요, 이건 내가 찾고 있던 것입니다. – Neeta
사실 그가 진술 한 이유 때문에이 문제를 해결하는 올바른 방법 일 것입니다. 어딘가에 제품 목록을 캐시하여 리디렉션 할 때마다 데이터베이스를 호출하지 않아도되지만 별도의 주제입니다. –
캐싱에 대해 어떻게 생각합니까? 또는 저를 일부 리소스로 리디렉션 할 수 있습니까? – Neeta