【问题标题】:Model value is getting null when form post表单发布时模型值变为空
【发布时间】:2013-09-20 19:25:46
【问题描述】:

我填充我的模型并将该模型传递给操作方法中的视图。在视图中使用 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 属性,我不会在循环中生成单选按钮,当我选择任何单选按钮并单击表单提交时,表单数据会正确反序列化为我的模型 in action 方法,但是当我检查 sex 属性时,我看到了它显示空值。我是 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" />
}

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

【问题讨论】:

标签: asp.net-mvc-3


【解决方案1】:

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

喜欢这里

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

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

【讨论】:

  • 你能纠正我的完整代码,这样我就可以理解了,因为我是 MVC 的新手。
  • 如果我构建视图模型,那么我想我们确实需要在 StudentModel 类中有性别参考....我说的对吗?
  • 我已经为你做了更改,只是更改然后询问你的问题是否没有解决并提交你得到的错误,如果你得到了答案,那么请勾选我的答案
猜你喜欢
  • 2023-03-31
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-28
  • 2022-01-08
  • 2017-01-24
  • 2018-02-20
相关资源
最近更新 更多