代码优化C#ASP MVC [英] Code optimization C# ASP MVC

查看:105
本文介绍了代码优化C#ASP MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这些是处理控制器中图片上传的方法。



So these are the methods that handle picture upload in the controller .

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
              
          
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("/Content/Images/") + file.FileName);
                    album.AlbumArtUrl = ("/Content/Images/") + file.FileName;
                }
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            return View(album);
        }
















[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
       {
           if (ModelState.IsValid)
           {
               if (file != null)
               {
                  file.SaveAs(HttpContext.Server.MapPath("/Content/Images/")+file.FileName);
                   album.AlbumArtUrl = ("/Content/Images/")+file.FileName;
               }


               db.Albums.Add(album);
               db.SaveChanges();
               return RedirectToAction("Index");
           }

           ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
           ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
           return View(album);
       }









SO basicaly第一行方法是相同的,我想要的是创建一个名为Upload的单独方法,并在Create()和Edit()中调用它。是可能的,任何人都可以指导我或告诉我如何?谢谢。



我尝试了什么:



我在上面解释了问题。





SO basicaly first lines of methods are the same , and what i want is to create a separate method called Upload , and call it inside Create() and Edit () . Is is possible , could anyone guide me or tell me how ? Thanks.

What I have tried:

I explained above the question.

推荐答案

试试



try

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
        {
            return CreateEdit(file, album, false);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
        {
            return CreateEdit(file, album, true);
        }

        private ActionResult CreateEdit(HttpPostedFileBase file,Album album,  bool isCreate)
        {
            if (ModelState.IsValid)
            { 
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("/Content/Images/") + file.FileName);
                    album.AlbumArtUrl = ("/Content/Images/") + file.FileName;
                }
                if(isCreate)
                    db.Albums.Add(album);
                else
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }


            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            return View(album);
        }


这篇关于代码优化C#ASP MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆