Exemplo n.º 1
0
        //lai dabūtu failu, ko var pievienot serverī, pievieno HttpPosted.... + to, ko ievadijām pie EditCats apakšā name=uploadedPicture
        public ActionResult EditCats(CatProfile catProfile, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(catProfile));
            }
            using (var CatDb = new CatDb())
            {
                var profilePic = new File();
                //saglabājam bildes faila nosaukumu
                profilePic.FileName    = System.IO.Path.GetFileName(uploadedPicture.FileName);
                profilePic.ContentType = uploadedPicture.ContentType;

                //BinaryReader pārvērš bildi baitos
                using (var reader = new System.IO.BinaryReader(uploadedPicture.InputStream))
                {
                    //saglabājam baitus datubāzes ierakstā
                    profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                }

                profilePic.CatProfileId = catProfile.CatID;
                profilePic.CatProfile   = catProfile;

                CatDb.Files.Add(profilePic);

                catProfile.ProfilePicture = profilePic;

                CatDb.Entry(catProfile).State = EntityState.Modified;
                CatDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            CatProfile profile = db.CatProfiles.Find(id);

            db.CatProfiles.Remove(profile);
            db.SaveChanges();
            return(RedirectToAction("List"));
        }
Exemplo n.º 3
0
        public ActionResult AddCat(CatProfile userCreatedCat, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }

            // izveido savienojumu ar datubāzi
            using (var catDb = new CatDB())
            {
                // pievienojam kaķi kaķu tabulā
                catDb.CatProfiles.Add(userCreatedCat);

                // saglabājam izmaiņas datubāzē
                catDb.SaveChanges();

                // ja ir pievienota profila bilde
                if (uploadedPicture != null)
                {
                    // izveidojam jaunu profila bildes datubāzes eksemplāru, ko ierakstīsim datubāzē
                    var profilePic = new File();

                    // saglabājam bildes faila nosaukumu
                    profilePic.FileName = Path.GetFileName(uploadedPicture.FileName);

                    // saglabājam bildes tipu
                    profilePic.ContentType = uploadedPicture.ContentType;

                    // izmantojam BinaryReader lai pārvērstu bildi baitos
                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        // saglabājam baitus datubāzes ierakstā
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    // pasakam profila bildei, kurš kaķa profils ir kaķa profils, kam šī bilde pieder
                    profilePic.CatProfileId = userCreatedCat.CatId;
                    profilePic.CatProfile   = userCreatedCat;

                    // pievienojam profila bildes datubāzes ierakstu Files tabulai
                    catDb.Files.Add(profilePic);

                    // saglabājam profila bildi datubāzē, lai iegūtu FileId priekš profila bildes ieraksta
                    catDb.SaveChanges();

                    // paskam kaķu profilam, kas ir viņa profila bilde
                    userCreatedCat.ProfilePicture = profilePic;

                    // saglabājam izmaiņas datubāzē
                    catDb.SaveChanges();
                }
            }

            // pavēlam browserim atgriezties Index lapā (t.i. pārlādēt to)
            return(RedirectToAction("Index"));
        }
 public ActionResult Create([Bind(Include = "name, age, color, gender,  breed")] CatProfile @catProfile)
 {
     if (ModelState.IsValid)
     {
         db.CatProfiles.Add(@catProfile);
         db.SaveChanges();
         return(RedirectToAction("List"));
     }
     return(View());
 }
Exemplo n.º 5
0
        public ActionResult EditCat(CatProfile catProfile, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(catProfile));
            }

            using (var catDb = new CatDB())
            {
                // ja ir pievienota profila bilde
                if (uploadedPicture != null)
                {
                    // atrodam šobrīdējo bildi, ja tāda ir
                    var currentPic = catDb.Files.FirstOrDefault(fileRecord => fileRecord.CatProfileId == catProfile.CatId);
                    if (currentPic != null)
                    {
                        catDb.Files.Remove(currentPic);
                    }

                    // izveidojam jaunu profila bildes datubāzes eksemplāru, ko ierakstīsim datubāzē
                    var profilePic = new File();

                    // saglabājam bildes faila nosaukumu
                    profilePic.FileName = Path.GetFileName(uploadedPicture.FileName);

                    // saglabājam bildes tipu
                    profilePic.ContentType = uploadedPicture.ContentType;

                    // izmantojam BinaryReader lai pārvērstu bildi baitos
                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        // saglabājam baitus datubāzes ierakstā
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    // pasakam profila bildei, kurš kaķa profils ir kaķa profils, kam šī bilde pieder
                    profilePic.CatProfileId = catProfile.CatId;
                    profilePic.CatProfile   = catProfile;

                    // pievienojam profila bildes datubāzes ierakstu Files tabulai
                    catDb.Files.Add(profilePic);

                    // paskam kaķu profilam, kas ir viņa profila bilde
                    catProfile.ProfilePicture = profilePic;
                }

                // pievienot using System.Data.Entity;
                catDb.Entry(catProfile).State = EntityState.Modified;
                catDb.SaveChanges();
            }

            // pavēlam browserim atgriezties Index lapā (t.i. pārlādēt to)
            return(RedirectToAction("Index"));
        }
Exemplo n.º 6
0
        public ActionResult AddCats(CatProfile userCreatedCat)
        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }
            using (var CatDb = new CatDb())
            {
                CatDb.CatProfiles.Add(userCreatedCat);
                CatDb.SaveChanges();

                return(RedirectToAction("Index"));
            }
        }
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            CatProfile profile = db.CatProfiles.Find(id);

            if (id == null)
            {
                return(HttpNotFound());
            }
            return(View(profile));
        }
Exemplo n.º 8
0
        public ActionResult AddCat(CatProfile userCreatedCat)

        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }
            // izveido savienojumu ar datu bazi
            using (var catDb = new catDb())
            {
                catDb.CatProfiles.Add(userCreatedCat);
                catDb.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }
Exemplo n.º 9
0
        public ActionResult EditCat(CatProfile catProfile, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(catProfile));
            }
            using (var catDb = new CatDb())
            {
                if (uploadedPicture != null)
                {
                    var currentPic = catDb.Files.FirstOrDefault(fileRecord => fileRecord.CatProfileIe == catProfile.CatID);
                    if (currentPic != null)
                    {
                        catDb.Files.Remove(currentPic);
                    }



                    var profilePic = new UploadedFiles.File();
                    profilePic.FileName    = Path.GetFileName(uploadedPicture.FileName);
                    profilePic.ContentType = uploadedPicture.ContentType;

                    profilePic.CatProfileIe = catProfile.CatID;
                    profilePic.CatProfile   = catProfile;

                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    catDb.Files.Add(profilePic);

                    catProfile.profilePicture = profilePic;
                }
                catDb.Entry(catProfile).State = System.Data.Entity.EntityState.Modified;
                catDb.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 10
0
        public ActionResult AddCat(CatProfile userCreatedCat, HttpPostedFileBase uploadedPicture)
        {
            if (ModelState.IsValid == false)
            {
                return(View(userCreatedCat));
            }

            using (var catDb = new CatDb())
            {
                catDb.CatProfiles.Add(userCreatedCat);

                catDb.SaveChanges();

                if (uploadedPicture != null)
                {
                    var profilePic = new UploadedFiles.File();

                    profilePic.FileName = Path.GetFileName(uploadedPicture.FileName);

                    profilePic.ContentType = uploadedPicture.ContentType;

                    using (var reader = new BinaryReader(uploadedPicture.InputStream))
                    {
                        profilePic.Content = reader.ReadBytes(uploadedPicture.ContentLength);
                    }

                    profilePic.CatProfileIe = userCreatedCat.CatID;
                    profilePic.CatProfile   = userCreatedCat;

                    catDb.Files.Add(profilePic);
                    catDb.SaveChanges();

                    userCreatedCat.profilePicture = profilePic;

                    catDb.SaveChanges();
                }
            }

            return(RedirectToAction("Index"));
        }