[RESOLVED]Value cannot be null. Parameter name: items (DrodownList)

My Register.cshtml below

<div class="form-group">
                                    @Html.LabelFor(model => model.UyeSehir, new { @class = "col-lg-4 control-label" })
                                    <div class="col-lg-8">
                                       @Html.DropDownListFor(model => Model.UyeSehir, new SelectList(ViewBag.Sehirs as System.Collections.IEnumerable, "Id", "SehirAd"),
            "Sehir Seç", new { id = "ddlSehirs" }) //Error Line
                                    </div>
                                </div>

Error is stack details

   at System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, String dataGroupField, IEnumerable selectedValues, IEnumerable disabledValues, IEnumerable disabledGroups)
   at System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, String dataGroupField, IEnumerable selectedValues, IEnumerable disabledValues)
   at System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, String dataGroupField, IEnumerable selectedValues)
   at System.Web.Mvc.MultiSelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, IEnumerable selectedValues)
   at System.Web.Mvc.SelectList..ctor(IEnumerable items, String dataValueField, String dataTextField, Object selectedValue)
   at System.Web.Mvc.SelectList..ctor(IEnumerable items, String dataValueField, String dataTextField)
     at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.StartPage.RunPage()
   at System.Web.WebPages.StartPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

 Error Image

http://send-picture.com/img-5474a31a6aadc.html

AccountController ( Register Actin Method )

public ActionResult Register()
        {            
            
            ViewBag.Sehirs = _db.Sehirs.ToList();
            ViewBag.Ilces = _db.Ilces.ToList();
            ViewBag.Semts = _db.Semts.ToList();

            return View();
        }

Model page

        [Display(Name = "Sehir")]
        public int UyeSehir { get; set; }

        [Display(Name = "İlçe")]
        public int UyeIlce { get; set; }

        [Display(Name = "Semt")]
        public int UyeSemt { get; set; }

What is problem please help me

Are you sure that your collections that you are populating within your Controller actually have values in them? I would double check that first to ensure that they aren’t null (as your SelectList will be expecting some type of collection).

Additionally, you could likely simplify this by storing your SelectList objects in the ViewBag as seen below :

public ActionResult Register()
{            
       // Populate your ViewBag collections
       ViewBag.Sehirs = new SelectList(_db.Sehirs.ToList(), "Id", "SehirAd");
       ViewBag.Ilces = new SelectList(_db.Ilces.ToList(), "Id", "IlceAd");
       ViewBag.Semts = new SelectList(_db.Semts.ToList(), "Id", "SemtAd");

       return View();
}

And then just using the ViewBag in the View :

@Html.DropDownListFor(model => Model.UyeSehir, ViewBag.Sehirs as SelectList, "Sehir Seç", new { id = "ddlSehirs" })

I changed my code to your code but now another error

The ViewData item that has the key ‘UyeSehir’ is of type ‘System.Int32′ but must be of type ‘IEnumerable<SelectListItem>’.

Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The ViewData item that has the key ‘UyeSehir’ is of type ‘System.Int32′ but must be of type ‘IEnumerable<SelectListItem>’.

Source Error:

I resolved My proplem First Step change GET Register Method

        public ActionResult Register()
        {
        
            var dQuery = (from d in _db.Sehirs
                         orderby d.SehirAd
                         select d).ToList();

            ViewBag.SehirID = new SelectList(dQuery, "Id", "SehirAd");      
 


            ViewBag.Ilces = new SelectList(_db.Ilces.ToList(), "Id", "IlceAd");
            ViewBag.Semts = new SelectList(_db.Semts.ToList(), "Id", "SemtAd");

            return View();
        }

First Step change POST Register Method I added ViewBag.SehirID

 public async Task<ActionResult> Register(RegisterViewModel model)
        {
            var dQuery = (from d in _db.Sehirs
                          orderby d.SehirAd
                          select d).ToList();

            ViewBag.SehirID = new SelectList(dQuery, "Id", "SehirAd");

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, UyeAd = model.UyeAd, UyeSoyAd = model.UyeSoyAd, UyeFirma = model.UyeFirma, UyeTel = model.UyeTel, UyeCep = model.UyeCep, UyeSehir = model.UyeSehir, UyeIlce = model.UyeIlce, UyeSemt = model.UyeSemt, UyeAdres = model.UyeAdres};
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

ıt’s work problem resolved

Leave a Reply