c# - 没有类型为 'IEnumerable<SelectListItem>' 且键为 'Register.CountryId' 的 ViewData 项

标签 c# asp.net-mvc-4 razorengine razor-declarative-helpers

代码可以正常工作,但我没有收到错误:

There is no ViewData item of type 'IEnumerable' that has the key '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)

}

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

        }

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

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

我已经搜索了StackOverflow,但我还没有找到解决我的问题的答案。

最佳答案

我无法将其添加为注释,但您需要在 Register 操作的 POST 版本中返回 View 模型

    [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.IsValidTRUE,您的代码以前可能可以正常工作。也许您没有通过此检查,并且下面的代码在运行时失败。

关于c# - 没有类型为 'IEnumerable<SelectListItem>' 且键为 'Register.CountryId' 的 ViewData 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25482056/

相关文章:

c# - 数据列表控制文本搜索

c# - 如何限制在文本框中粘贴的字符数?

c# - 如何将枚举从数据库加载到 MVC4 模型?

c# - MVC : How do you give a viewmodel a list and correctly output it on . cshtml

c# - 单元测试错误 "Object reference not set to an instance of an object."

javascript - 如何在 html.actionlink 中仅调用 javascript 函数

css - 使用 Bootstrap 导航不适用于移动设备

c# - VS2015 在 RazorEngine 模板中看不到 Linq

C#/.NET 项目 - 我的设置是否正确?

c# - 发生错误 .", ExceptionMessage: "提供的 'HttpContent' 实例无效