1

我填充我的模型并将该模型传递给操作方法中的视图。在视图中使用 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);
        }

基于 Sex 属性,我在循环中不生成单选按钮,当我选择任何单选按钮并单击表单提交时,表单数据在操作方法中正确反序列化到我的模型,但是当我检查 sex 属性时,我看到它显示为 null价值。我是 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" />
}

还指导我如何对我的性财产设置验证。如果有人尝试在不选择任何单选按钮的情况下提交表单,则会显示一条消息。谢谢,请指导我犯错的地方。

4

1 回答 1

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

在提交时还将视图模型发布到您的发布方法

于 2015-04-01T15:41:46.093 回答