مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

sinahzh

عضویت از 1397/05/27

بر قراری ارتباط بین جداول

  • یکشنبه 29 تیر 1399
  • 16:30
تشکر میکنم

سلام من دوتا جدول دارم. جدول اولم مال محصولاتم و جدول دومم مال عکسای محصولات می باشد. و میخوام ارتباط بین این دوتا جدول ایجاد بکنم.

خب من بجای یک عکس نیاز به 5 تا عکس دارم و آمدم یک فیلد درست کردم بنام imageId و Id عکسارو از جدول تصاویرم میگیرم و در این جدول ذحیره میکنم.

مثلا در یک فیلد ImageId اطلاعات Id عکس به اینگونه ذخیره شده اند: imageId: 1,2,3,4  

1,2,3,4: Id تصاویر می باشد که در جدول محصولات و فیلد ImageId ذخیره شده اند

حالا من میخوام محصولاتم و در صفحه اصلی و ادامه ی مطلب به نمایش بزارم. و نیاز به تصاویر جدول تصاویرم دارم و نمیدونم باید چیکار بکنم...!؟

خب من دوست دارم در کنترلر محصولات اکشن View فیلد imageId و Split بکنم و در جدول تصاویرم، تصاویری که Id شون در فیلد iamgeId برابر هم هستند و پیدا بکنم و با محصولاتم Return View بکنم. شما اگر میدونید لطفا راهنمایی بکنید.

جدول محصولاتم: 

    public partial class Page
    {
        public int PageId { get; set; }
        public string CategoryId { get; set; }
        public string Title  { get; set; }
        public double price { get; set; }
        public double off { get; set; }
        public string LongDescription { get; set; }
        public int Visit { get; set; }
        public string ImageCover { get; set; }
        public string ImageId { get; set; }
        public bool ShowInSlider { get; set; }
        public string Date { get; set; }
        public virtual List<Comment> Comments { get; set; }




    }

    public partial class Page
    {
        #region operations
        EF entity = new EF();
        public string Create(Page record)
        {
            entity.Pages.Add(record);
            try { entity.SaveChanges(); return "ok"; }
            catch { return "Nok"; }
        }
        public List<Page> Read()
        {
            return entity.Pages.ToList();
        }
        public Page Read(int id)
        {
            // return entity.Posts.Where(item => item.Id == id).Single();
            return entity.Pages.Find(id);
        }

        public string update(Page newRecord)
        {
            entity.Pages.Attach(newRecord);
            entity.Entry(newRecord).State = System.Data.Entity.EntityState.Modified;
            try
            {
                entity.SaveChanges();
                return "ok";
            }
            catch
            {
                return "Nok";
            }
        }

        public string delete(int id)
        {
            Page temp = entity.Pages.Find(id);
            entity.Pages.Remove(temp);
            try
            {
                entity.SaveChanges(); return "ok";
            }
            catch
            {
                return "Nok";
            }
        }

        public string delete()
        {
            entity.Pages.RemoveRange(Read());
            try
            {
                entity.SaveChanges(); return "ok";
            }
            catch
            {
                return "Nok";
            }
        }

        #endregion
    }
}

جدول آپلود تصاویر:

[Table("UploadFile")]
    public partial class UploadFile
    {
        public int Id { get; set; }

        [StringLength(50)]
        public string Path { get; set; }

        public int Size { get; set; }

        [StringLength(10)]
        public string Date { get; set; }
    }


    public partial class UploadFile
    {
        #region operations
        EF entity = new EF();
        public string Create(UploadFile record)
        {
            entity.UploadFiles.Add(record);
            try { entity.SaveChanges(); return "ok"; }
            catch { return "Nok"; }
        }
        public List<UploadFile> Read()
        {
            return entity.UploadFiles.ToList();
        }
        public UploadFile Read(int id)
        {
            // return entity.Posts.Where(item => item.Id == id).Single();
            return entity.UploadFiles.Find(id);
        }

        public string update(UploadFile newRecord)
        {
            entity.UploadFiles.Attach(newRecord);
            entity.Entry(newRecord).State = System.Data.Entity.EntityState.Modified;
            try
            {
                entity.SaveChanges(); return "ok";
            }
            catch
            {
                return "Nok";
            }
        }

        public string delete(int id)
        {
            UploadFile temp = entity.UploadFiles.Find(id);
            entity.UploadFiles.Remove(temp);
            try
            {
                entity.SaveChanges(); return "ok";
            }
            catch
            {
                return "Nok";
            }
        }

        public string delete()
        {
            entity.UploadFiles.RemoveRange(Read());
            try
            {
                entity.SaveChanges(); return "ok";
            }
            catch
            {
                return "Nok";
            }
        }

        #endregion
    }
}

کد کنترلر محصولاتم:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using tackService.Models;
using System.Globalization;

namespace tackService.Areas.panel.Controllers
{
    public class PageController : Controller
    {
        Page operation = new Page();
        UploadFile operationUploadFile = new UploadFile();


        // GET: panel/Page
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Page data)
        {
            PersianCalendar timePersian = new PersianCalendar();
            operation.Date = timePersian.GetYear(DateTime.Now) + "/" + timePersian.GetMonth(DateTime.Now) + "/" + timePersian.GetDayOfMonth(DateTime.Now);
            operation.Title = data.Title;
            operation.CategoryId = data.CategoryId;
            operation.ImageId = data.ImageId;
            operation.LongDescription = data.LongDescription;
            operation.price = data.price;
            operation.off = data.off;
            if (operation.Create(operation) == "ok")
            {
                ViewBag.msg2 = "1";
            }
            return View();
        }

        public ActionResult view()
        {
            return View(model: operation.Read());
        }
        public ActionResult edit(int? id)
        {
            return View(model: operation.Read(id.Value));
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult edit(Page data)
        {

            if (operation.update(data) == "ok")
            {
                ViewBag.msg2 = "1";
            }
            else ViewBag.msg2 = "2";
            return Redirect("view");
        }

        public ActionResult delete(int id)
        {
            operation.delete(id);
            return Redirect("/panel/page/view");
        }

    }
}

کد کنترلر تصاویرم:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using tackService.Models;
using System.Globalization;

namespace tackService.Areas.panel.Controllers
{
    public class UploadController : Controller
    {
        // GET: panel/Upload

        UploadFile Operation = new UploadFile();

        public ActionResult create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult create(HttpPostedFileBase file)
        {
            if (file.ContentLength < 704800) //کمتر از 2 مگابایت بصورت بیت حساب می شود
            {
                var fileName = Path.GetExtension(file.FileName);
                if (fileName.ToLower() == ".jpg" || fileName.ToLower() == ".png" || fileName.ToLower() == ".gif" || fileName.ToLower() == ".jpeg")
                {
                    PersianCalendar timePersian = new PersianCalendar();
                    string AddressFile = DateTime.Now.Millisecond + file.FileName;

                    Operation.Date = timePersian.GetYear(DateTime.Now) + "/" + timePersian.GetMonth(DateTime.Now) + "/" + timePersian.GetDayOfMonth(DateTime.Now);
                    Operation.Path = AddressFile;
                    Operation.Size = file.ContentLength;
                    if (Operation.Create(Operation) == "ok")
                    {
                        var path = Path.Combine(Server.MapPath("~/Areas/panel/assets/uploads/"), AddressFile);
                        file.SaveAs(path);
                        ViewData["msg"] = "با موفقیت آپلود شد";
                    }
                    else ViewData["msg"] = "با موفقیت آپلود نشد";

                }
                else ViewData["msg"] = "پسوند فایل غیر مجاز است";
            }
            else ViewData["msg"] = "با موفقیت آپلود نشد شاید حجم فایل بیشتر از مقدار تعیین شده می باشد";

            return View("create");
        }

        public ActionResult view()
        {
            return View(model: Operation.Read());
        }

        public ActionResult Delete(int id)
        {
            string oldImageUrl = Operation.Read(id).Path;
            string data = "~/Areas/panel/assets/uploads/" + oldImageUrl;
            System.IO.File.Delete(Server.MapPath(data));
            Operation.delete(id);
            return Redirect("/panel/upload/view");
        }
    }
}

پاسخ های این پرسش

تعداد پاسخ ها : 0 پاسخ
در حال حاضر هیچ پاسخی ارسال نشده است
کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)