[RESOLVED]drop down list for using mvc 5

I was looking for a dropdownlistforcexample and found this is what I need There is no ViewData item of type ‘IEnumerable<SelectListItem>’
that has the key ‘CategoryID’

but I can’t understand the "– Select –" in the view page, if it means the select statement, well it’s already mentioned in the controller, do I have to mention it again?

lolo512

I can’t understand the "– Select –" in the view page,

it is a "Please choose an item" text

what is this error?

There is no ViewData item of type ‘IEnumerable<SelectListItem>’ that has the key ‘category’.

this is my create.cshtml page:

@model sportsMania.News

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>create</title>
</head>
<body>
@using (Html.BeginForm("create", "News", FormMethod.Post,
new { enctype = "multipart/form-data" }))

{
<div>
Category<br />
@Html.DropDownListFor(item => Model.category, ViewBag.category as SelectList, "please select a category")
<br/>
Title<br />
@Html.TextBox("title") <br />
Content<br/>
@Html.TextBox("content") <br />
Image<br />
<input type="File" name="file" id="file" value="Choose File" />
<input type="submit" value="Upload" class="submit" />
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>
</body>
</html>

and this is my controller:

sportsEntities db = new sportsEntities();
        public ActionResult Index()
        {
            var data = from p in db.categories

                       select p.categoryName;
            
                SelectList list = new SelectList(data);
               
                ViewBag.Category = list;
                return View();
            
        }
        public ActionResult create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult create(HttpPostedFileBase file)
        {
            string category = Request.Form["category"];

            if (file != null)
            {

                string ImageName = System.IO.Path.GetFileName(file.FileName);
                // string physicalPath = Server.MapPath("~/images/" + ImageName);
                //  string physicalPath = "~/" + category + "/" + ImageName; // your code goes here
                var path = Path.Combine(Server.MapPath("~/images/" + category), ImageName);

                string subpath = "~/images/" + category;


                bool exists = System.IO.Directory.Exists(Server.MapPath(subpath));

                if (!exists)
                {
                    System.IO.Directory.CreateDirectory(Server.MapPath(subpath));
                }



                // save image in folder
                file.SaveAs(path);


                //save new record in database
                News newRecord = new News();
                newRecord.category= Request.Form["category"];
                newRecord.title = Request.Form["title"];
                newRecord.content = Request.Form["content"];
                newRecord.imagePath = ImageName;
                db.News.Add(newRecord);
                db.SaveChanges();


            }
            //Display records
            return RedirectToAction("Index");
        }

lolo512

public ActionResult create()
        {
            return View();
        }

Since you view generated from create action s, you need to create categories and attach to viewbag in create action

public ActionResult Create()
        {
            var data = from p in db.categories

                       select p.categoryName;
            
                SelectList list = new SelectList(data);
               
                ViewBag.Category = list;
                return View();
            
        }

Leave a Reply