Click here to Skip to main content
15,885,875 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work on Commercial project to Sale product on Internet I want to Create new project and Iam using Code First and make relation between product and category
and when try to create new product that message is shown to me

(
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Categoryid')

but I noticed something more mysterious
Although error shown to me but when I open list of product new product is saved after I 
Close error message
 and in this project I try to make photo to each product 
and when I clear and delete upload and photo the error Solved
So I want to upload photo to each product without  this ERROR

What I have tried:

my Action Result 
<pre>    public ActionResult Create()
        {
            var CategoryList = dbcontext.category.ToList();
            List<SelectListItem> CList = new List<SelectListItem>();
            foreach (var item in CategoryList)
            {
                CList.Add(new SelectListItem { Text = item.CategoryName, Value = item.CategoryID.ToString() });
            }
            ViewBag.Categoryid = CList;
            
            return View();
        }





[HttpPost]
public ActionResult Create(Product product, HttpPostedFileBase file)
{
    try
    {
        // TODO: Add insert logic here
        product.Image = file.FileName;
        dbcontext.product.Add(product);
        dbcontext.SaveChanges();

       file.SaveAs(Server.MapPath("/Upload" + file.FileName));
        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}







my View

@using (Html.BeginForm("Create","Products",FormMethod.Post,new {enctype="multipart/form-data" }))




@Html.DropDownList("Categoryid",(IEnumerable<SelectListItem>)ViewBag.Categoryid, htmlAttributes: new { @class = "form-control" })
Posted
Updated 5-Jun-19 23:19pm
Comments
F-ES Sitecore 6-Jun-19 5:25am    
Not related to your question but don't use code first, use database first. Code first will only cause you problems in the long run.

You have to populate the ViewBag every time you show the view. The simplest option is probably to move the code to a separate method.

You'll also want to log or display the actual exception details, so that you can track down what the problem is. My guess is that there's a problem saving the file to disk.

Also remember that some browsers send the full client path of the uploaded file, rather than just the file name. You'll want to make sure you're only using the file name part.
C#
private void PopulateLookups()
{
    var categoryList = dbcontext.category.ToList();
    var selectList = new List<SelectListItem>();
    foreach (var item in categoryList)
    {
        selectList.Add(new SelectListItem { Text = item.CategoryName, Value = item.CategoryID.ToString() });
    }
    ViewBag.Categoryid = selectList;
}

[HttpGet]
public ActionResult Create()
{
    PopulateLookups();
    return View();
}

[HttpPost]
public ActionResult Create(Product product, HttpPostedFileBase file)
{
    try
    {
        string fileName = System.IO.Path.GetFileName(file.FileName);
        string physicalPath = System.IO.Path.Combine(Server.MapPath("~/Upload/"), fileName);
        file.SaveAs(physicalPath);

        product.Image = fileName;
        dbcontext.product.Add(product);
        dbcontext.SaveChanges();
        
        return RedirectToAction("Index");    
    }
    catch (Exception ex)
    {
        ViewBag.Error = ex;
        PopulateLookups();
        return View();
    }
}
In the view:
Razor
@if (ViewBag.Error != null)
{
    <p><b>Error:</b></p>
    <pre>@ViewBag.Error</pre>
}
 
Share this answer
 
Hi MR Richard Deeming nice to meet you
the message is disappeared
but viewbag Error on my Screen is
ERROR:

System.NullReferenceException: Object reference not set to an instance of an object.
at MyShop.Controllers.ProductsController.Create(Product product, HttpPostedFileBase file) in C:\Users\W10\Desktop\MyShop\MyShop\Controllers\ProductsController.cs:line 62
 
Share this answer
 

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