代码之家  ›  专栏  ›  技术社区  ›  Jack

没有“IEnumerable<SelectListItem>”类型的ViewData项具有键“Register.CountryId”

  •  2
  • Jack  · 技术社区  · 11 年前

    代码正在运行,但我没有收到错误:

    没有类型为“IEnumerable”的ViewData项 具有密钥“Register.CountryId”

    在视图中 :

     @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)
    
    }
    

    在控制器中 :

    [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);
    
            }
    

    在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;
            }
    

    **注册型号**:

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

    我想从“视图模型”中执行此操作。

    我已经搜索过了 堆栈溢出 ,但我还没有找到解决我问题的答案。

    1 回复  |  直到 11 年前
        1
  •  2
  •   Christian Phillips    11 年前

    我无法将其添加为注释,但您需要在POST版本的 Register 行动

        [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
    
       }
    

    如果出现以下情况,您的代码可能以前一直在工作 !ModelState.IsValid 符合事实的 。可能您尚未通过此检查,下面的代码在运行时失败。