lundi 23 octobre 2017

LinQ to SQL missing data

Hi, I'm trying to get all rows from my SANPHAM table but somehow it keep missing some rows. Table has 9 rows in database, however, when displayed, it show only 5 rows (I have picture below, and don't mind about products' image, I will add the rests later).


I have tried to do something in my code, I change int i = 0 to int i = 1 and there are 2 situations. With i = 0, the result misses even rows and with i = 1, odd rows disappear.


I am a newbie to this, I looked for some solutions but still can't fix it, hope you guys can help

Here is my code:

HomeControllers.cs

namespace MvcApplication.Controllers
{ 
    public class HomeController : Controller
    {        
        private LinQtoSQLDataContext LinQtoSQLDataContext = new LinQtoSQLDataContext();
        public ActionResult Index()
        {
            IList<Models.SanPhamModels> sanPhamList = new List<Models.SanPhamModels>();
            var query = from sanpham in LinQtoSQLDataContext.SANPHAMs select sanpham;
            var sps = query.ToList();
            foreach (var sanPhamData in sps)
            {
                sanPhamList.Add(new Models.SanPhamModels()
                {
                    maSanPham = sanPhamData.maSanPham,
                    tenSanPham = sanPhamData.tenSanPham,
                    gia = sanPhamData.gia,
                    tacGia = sanPhamData.tacGia,
                    moTa = sanPhamData.moTa,
                    maDanhMuc = sanPhamData.maDanhMuc,
                    hinhAnh = sanPhamData.hinhAnh
                });
            }
            return View(sanPhamList);
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

Index.cshtml

@for (int i=0; i<Model.Count; i++)
{
    var tmp = "<div class=\"product_box\">";
    tmp += "<a href=\"" + Url.Action("Index", "productdetail", new { maSanPham = Model[i].maSanPham }) + "\"><img src=\""+ Url.Content(Model[i].hinhAnh) + "\" alt=\"Image\" /></a><br>";
    tmp += Html.ActionLink(Model[i].tenSanPham, "productdetail", new { maSanPham = Model[i].maSanPham }, null);
    tmp += "<p class=\'product_price\">" + Model[i].gia.ToString() + "</p>";
    tmp += Html.ActionLink("Add to Cart", "shoppingcart", new { maSanPham = Model[i].maSanPham }, new {@class = "add_to_card"});
    tmp += Html.ActionLink("Detail", "productdetail", new { maSanPham = Model[i].maSanPham }, new { @class = "detail" });
    tmp += "</div>";

    @Html.Raw(tmp)        
}

SanPhamModels

 namespace MvcApplication.Models
{
    public class SanPhamModels
    {
        public string maSanPham { get; set; }
        public string tenSanPham { get; set; }
        public double gia { get; set; }
        public string tacGia { get; set; }
        public string moTa { get; set; }
        public string maDanhMuc { get; set; }
        public string hinhAnh { get; set; }
    }
}

My database
My website result image




Aucun commentaire:

Enregistrer un commentaire