表单发布时模型值为null

时间:2013-09-20 19:25:47

标签: asp.net-mvc-3

我填充我的模型并将该模型传递给动作方法查看。在视图中使用for循环我根据模型生成单选按钮。

只看到这段代码。这里我手动填充我的模型并从操作方法传递给视图。

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

基于性别属性我在循环中没有生成单选按钮,当我选择任何单选按钮并单击表单提交然后表单数据正确反序列化到我的模型在操作方法但是当我检查性别属性然后我看到它显示空值。我是mvc的新手,并且无法理解为什么我的性别属性在表单提交时会变为空。

这是我的完整代码,请告诉我我在哪里弄错了。感谢

控制器代码

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(student);
        }

        [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);
        }
        public ActionResult About()
        {
            return View();
        }
    }

型号代码

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


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

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

这是我的观看代码。

@model MvcRadioButton.Models.StudentModel
@{
    ViewBag.Title = "Home Page";
}
@using(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, Sex.ID, new { @id = ("sex" + Sex.ID) })
                @*@Html.Label("Sex", sex.Type)*@
                @Html.Label("sex" + Sex.ID, Sex.Type)
            </div>
    }
   <input type="submit" value="Submit" />
}

还指导我如何设置我的性别属性的验证。如果有人试图提交表单而不选择任何单选按钮,则会显示一条消息。谢谢,请指导我犯错的地方。

1 个答案:

答案 0 :(得分:0)

您只是发布学生模型制作视图模型

喜欢这里

public class StudentDetailViewModel
    {
        public Student Student { get; set; }
        public Sex Sex { get; set; }
    }

并在索引方法中使用此模型来传递值以查看视图中的模型名称,如下所示

StudentDetailViewModel studentvm = new StudentDetailViewModel();
studentvm.Student.firstname = -----------;
studentvm.Sex.Type = ----------;
return View(studentvm);
@model MvcRadioButton.Models.StudentDetailViewModel

在提交时也会将viewmodel发布到你的帖子方法