遇到从模型生成单选按钮的错误

时间:2013-09-19 19:38:39

标签: asp.net-mvc-3

我正在尝试从模型生成单选按钮但是获取对象引用错误。我只是无法理解我在MVC中的错误,因为我只是在学习它。这是我的完整代码。请看看,告诉我哪里出错了。感谢

模型类

 public class StudentModel
    {

        [Required(ErrorMessage = "First Name Required")] // textboxes will show
        [Display(Name = "First Name :")]
        [StringLength(5, ErrorMessage = "First Name cannot be longer than 5 characters.")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last Name Required")] // textboxes will show
        [Display(Name = "Last Name :")]
        [StringLength(5, ErrorMessage = "Last Name cannot be longer than 5 characters.")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "Sex Required")]
        [Display(Name = "Sex :")]
        public int SexID { get; set; }

        public List<Sex> Sex { get; set; }

    }

    public class Sex
    {
        public string ID { get; set; }
        public string Type { get; set; }
    }

控制器类

 public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            var student = new StudentModel
            {
                FirstName = "Rion",
                LastName = "Gomes",

                //I think the best way to populate this list is to call a service here.
                Sex = new List<Sex>
                {
                    new Sex{ID="1" , Type = "Male"}, 
                    new Sex{ID="2" , Type = "Female"}
                }
            };

            return View();
        }

        [HttpPost]
        public ActionResult Index(StudentModel model)
        {
            if (ModelState.IsValid)
            {
                //TODO: Save your model and redirect 
            }

            //Call the same service to initialize your model again (cause we didn't post the list of sexs)
            return View(model);
        }
}

查看代码

@model MvcRadioButton.Models.StudentModel

@Html.BeginForm()
{
    <div>
        @Html.LabelFor(model => model.FirstName)
        @Html.EditorFor(model => model.FirstName)
        @Html.ValidationMessageFor(model => model.FirstName)
    </div>
    <div>
        @Html.LabelFor(model => model.LastName)
        @Html.EditorFor(model => model.LastName)
        @Html.ValidationMessageFor(model => model.LastName)
    </div>
    @{ 
        foreach (var sex in Model.Sex)
        {
            <div>
                @Html.RadioButtonFor(model => model.Sex, new { id = "sex" + sex.ID })
                @Html.Label("sex" + sex.ID, sex.Type)
            </div>
        }
    }

    <input type="submit" value="Submit" />
}

此行抛出错误

foreach (var sex in Model.Sex) saying model null or object reference error

1 个答案:

答案 0 :(得分:0)

最初投放网页时,您永远不会将模型返回到您的视图。因此,当您尝试迭代Sex对象时,会出现空引用错误。

[HttpGet]
public ActionResult Index()
{
    ... code ...
    return View(); //The problem is here!
}

您应该将模型返回到您的视图中:

[HttpGet]
public ActionResult Index()
{
    ... code ...
    return View(student);
}