Search code examples
c#asp.net-mvc-4razorenginerazor-declarative-helpers

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Register.CountryId'


The code was working, but I am not getting the error:

There is no ViewData item of type 'IEnumerable' that has the key 'Register.CountryId'

In view:

 @using (Html.BeginForm((string)ViewBag.FormAction, "NMPAccount", new { @id = "myForm", @name = "myForm" }))

 @Html.LabelFor(m => m.Register.CountryId)
 @Html.DropDownListFor(m => m.Register.CountryId, Model.Register.CountryList,  
                                                    new { @id = "CountryList" })
 @Html.ValidationMessageFor(m => m.Register.CountryId)

}

In controller:

[AllowAnonymous]
public ActionResult Register() {
    var registerViewModel = new RegisterViewModel();
    registerViewModel.Initalize();
    return View("Register", registerViewModel);

}

    [AllowAnonymous]
    [HttpPost]
    public ActionResult Register(RegisterViewModel model) {
        if (!ModelState.IsValid) {
            model.Initalize();
            ModelState.AddModelError(string.Empty, "");
            return View("Register", model);

        }

        if (ModelState.IsValid && model.Register != null) {
            if (RegisterViewModel.RegisterNewUser(model.Register))
                return RedirectToAction("RegisterSuccess");
            else
                ModelState.AddModelError("", NMPAccountResource.Failed);

        }

in ViewModel:

public class RegisterViewModel {
        public RegisterViewModel() {
            Register = new RegisterModel();
        }

        public RegisterModel Register { get; set; }

        public void Initalize() {
            var registerUser = new RegisterUser();
            IList<CountryCodes> couList = Utilities.GetCountryList(languageId);
            var countryList = (from data in couList
                               select new CustomItem() {
                                   Id = data.CountryCodesId,
                                   Description = data.CountryName

                               }).ToList();
            if (countryList.Count > 0) {
                countryList[0].Id = null;
            }


            var clist = new SelectList(countryList, "Id", "Description");

            Register.CountryList = clist;
        }

** Register Model**:

   public string CountryId { get; set; }
   public IEnumerable<SelectListItem> CountryList { get; set; } 

I want to do this from "view model".

I have searched StackOverflow, but I have not found an answer to solve my problem.


Solution

  • I can't add this as a comment, but you need to return a view model in the POST version of the Register Action

        [AllowAnonymous]
            [HttpPost]
            public ActionResult Register(RegisterViewModel model) {
                if (!ModelState.IsValid) {
                    model.Initalize();
                    ModelState.AddModelError(string.Empty, "");
                    return View("Register", model);
    
                }
    
                if (ModelState.IsValid && model.Register != null) {
                    if (RegisterViewModel.RegisterNewUser(model.Register))
                        return RedirectToAction("RegisterSuccess");
                    else
                    {
                        ModelState.AddModelError("", NMPAccountResource.Failed);
                   //there needs to be code here to return the view.
                   return View("Register", model); //<--- this is missing
                   }
    
                }
                   //there needs to be code here to return the view.
                   return View("Register", model); //<--- this is missing
    
       }
    

    Your code might have been working previously if the condition !ModelState.IsValid was TRUE. Maybe you've not got passed this check, and the code below fails at run-time.