コード例 #1
0
        public ActionResult UpPost()
        {
            var db = new ShareImageDbContext();

            ViewData["CategoryID"] = new SelectList(db.Categories.ToList(), "CategoryID", "CategoryName");
            //List<Category> l = db.Categories.ToList();
            //SelectList SL = new SelectList(l.AsEnumerable(), "CategoryID", "categoryName");
            //ViewBag.CatList = SL;

            return(View());
        }
コード例 #2
0
        public ActionResult UpPost(Post model, HttpPostedFileBase txtImg)
        {
            var db = new ShareImageDbContext();

            if (ModelState.IsValid)
            {
                if (txtImg != null)
                {
                    //FileInfo fi = new FileInfo(txtImg.FileName);
                    //var user=new User();
                    //txtImg.SaveAs(HttpContext.Server.MapPath("~/Images/")
                    //                                    + txtImg.FileName);
                    var fileName = Path.GetFileName(txtImg.FileName);
                    var path     = Path.Combine(Server.MapPath("~/Images"), fileName);
                    txtImg.SaveAs(path);
                    path = Url.Content(Path.Combine("~/Images", fileName));
                    var post = new Post();

                    post.PostID      = model.PostID;
                    post.Title       = model.Title;
                    post.Description = model.Description;
                    post.CategoryID  = model.CategoryID;    //Đây là dòng cần lấy CategoryID
                    //Dưới đây là code lấy UserID hiện tại đang đăng nhập.
                    var userSession = new UserLogin();
                    userSession     = (UserLogin)Session[ShareImage.Common.CommonConstants.USER_SESSION];
                    post.UserID     = userSession.UserID;
                    post.CreateDate = DateTime.Now;
                    //post.Picture = txtImg.FileName;
                    post.Picture = path;
                    //var result = dao.Insert(post);
                    db.Posts.Add(post);
                    //db.Posts.Add(user);
                    db.SaveChanges();


                    return(RedirectToAction("Index", "Homeuser"));
                }

                else
                {
                    ModelState.AddModelError("", "Đăng ảnh thất bại!");
                }
            }
            return(View(model));
        }
コード例 #3
0
        // GET: Homeuser
        public ActionResult Index()
        {
            var db = new ShareImageDbContext();

            //var model = db.Posts.ToList();
            List <PostViewModel> model = new List <PostViewModel>();

            var q = (from a in db.Posts
                     join b in db.Users on a.UserID equals b.UserID
                     //from rt in t.DefaultIfEmpty()
                     select new
            {
                postID = a.PostID,
                title = a.Title,
                description = a.Description,
                createDate = a.CreateDate,
                picture = a.Picture,
                username = b.Username,
            }).ToList();

            foreach (var item in q)
            {
                model.Add(new PostViewModel()
                {
                    PostID      = item.postID,
                    Title       = item.title,
                    Description = item.description,
                    Picture     = item.picture,
                    CreateDate  = item.createDate,
                    Username    = item.username,
                });
            }

            return(View(model));
            //return View(q.OrderBy(x => x.CreateDate).ToList());
        }
コード例 #4
0
 public ImageController(ShareImageDbContext context)
 {
     _context = context;
 }
コード例 #5
0
 public HomeController(ILogger <HomeController> logger, ShareImageDbContext context)
 {
     _context = context;
     _logger  = logger;
 }