Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I Can't do Function Create in asp core mvc, i make one VIew for Edit And Create but functon Edit is Working Well, but function Create didn't Save in database

What I have tried:

my class that I put my function
C#
namespace books.BL
{
    public class ClsItem
    {
        public  StoreBooksContext booksContext=new StoreBooksContext();      

        public List<tbitem> GetAll()
        {
        
            List<tbitem> lstItems = booksContext.TbItems.
                OrderByDescending(a => a.ItemName).
                ThenByDescending(a => a.SalePrice).ToList();
            return lstItems;
        }

        public TbItem GetById(int id)
        {
            TbItem SelectedItem = booksContext.TbItems.FirstOrDefault(i => i.ItemId == id);
            return SelectedItem;
         
        }

        public bool AddToDataBase(TbItem item)
        {

            try
            {
                booksContext.TbItems.Add(item);
                booksContext.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public bool Edit(TbItem item)
        {

            try
            {

                booksContext.Entry(item).State = EntityState.Modified;
                booksContext.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
my Controller
C#
namespace books.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class ItemsController : Controller
    {
        public ClsItem clsItem = new ClsItem();

        public IActionResult List()
        {

            return View(clsItem.GetAll());
        }

        public IActionResult Edit(int? id)
        {
            ClsCategories clsCategories = new ClsCategories();
            ViewBag.clsCategoryList = clsCategories.GetAll();
            if (id != null)
            {
                return View(clsItem.GetById(Convert.ToInt32(id)));
            }
            else
            {
                return View();
            }

        }


     
        [HttpPost]
        public async Task <iactionresult> Save(TbItem item,List<iformfile> Files)
        {

            try
            {
                if (ModelState.IsValid)
                {

                    foreach (var file in Files)
                    {
                        if (file != null)
                        {
                            string ImageName = Guid.NewGuid().ToString() + ".jpg";
                            var filePaths = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Uploads", ImageName);
                            using (var stream = System.IO.File.Create(filePaths))
                            {
                                await file.CopyToAsync(stream);
                            }
                            item.ImageName = ImageName;
                        }
                    }

                    if (item.ItemId == 0)
                        clsItem.AddToDataBase(item);
                           
                    else
                    
                        clsItem.Edit(item);
                        return RedirectToAction("List", "Items");
                    
                }

                return RedirectToAction("List", "Items");
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
    }
}
Posted
Updated 20-May-21 6:18am
v2
Comments
SeanChupas 18-May-21 16:58pm    
1. Some of your code is not helpful and clutters the question. We do not need to see your Edit or List methods, for example. Clean up and format the code so it is much easier to read.
2. I don't see where your code gets called. You do have a Save method so that is what I would expect should be running so put a breakpoint in it and see if it is even running. You may not be calling Save in your view or your ViewModel might not be valid. You have to do some troubleshooting and debugging.
SeanChupas 18-May-21 16:58pm    
And what does your title have to do with this? Something about a hidden field?
Richard Deeming 20-May-21 12:20pm    
catch (Exception ex)
{
    throw ex;
}

Don't do that! You've just destroyed the stack trace, making it almost impossible to know where your exception was thrown from.

If you really need to rethrow an exception, just use throw; instead of throw ex;, as you have done in your ClsItem class.

But since none of these catch blocks do anything with the exception they've caught, you should simply remove the try..catch block, and let the exception propagate to the called unimpeded.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900