提交表单后模型中的对象列表为空

马塔克斯

在 cshtml 文件中,我正在遍历一个包含对象的列表并将值绑定到文本框。这是我的部分观点。

@model BotelHotel.Models.ViewModels.BookingViewModel
<div>
    @using (Html.BeginForm("StepThree", "Book", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {

        @Html.LabelFor(model => model.BookingDate)<br />
        @Html.TextBoxFor(model => model.BookingDate, new { @class = "form-control datepicker", placeholder = "Datum Boeking", @readonly = true })

        @Html.LabelFor(model => model.AmountOfPersons)<br />
        @Html.TextBoxFor(model => model.AmountOfPersons, new { @class = "form-control", @readonly = true })<br />

        for (int i = 0; i < Model.persons.Count; i++)
        {
            @Html.LabelFor(model => model.persons[i].Name)<br />
            @Html.TextBoxFor(model => model.persons[i].Name, new { @class = "form-control"})<br />
        }

        <button class="btn btn-success">Submit</button>
    }
</div>

单击提交按钮后,我在 StepThree 函数处有一个断点。该列表persons为空。为什么它是空的任何理由?stackoverflow 的其余部分似乎在说这是一个很好的解决方案。但它对我不起作用。

这是我的模型。

public class BookingViewModel
    {
        public string step { get; set; }
        public int roomID { get; set; }
        public List<PersonViewModel> persons = new List<PersonViewModel>();

        public BookingViewModel()
        {

        }

        public BookingViewModel(int roomID)
        {
            this.roomID = roomID;
            this.step = "PartialOne";
        }

        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        [Display(Name = "Datum")]
        public DateTime BookingDate { get; set; }

        [Required]
        [Display(Name = "Aantal personen")]
        public int AmountOfPersons { get; set; }

    }
public class PersonViewModel
    {

        private Person _person;

        public PersonViewModel()
        {
            _person = new Person();
        }

        public PersonViewModel(Person person)
        {
            _person = person;
        }


        public int Id { get; set; }
        [Required]
        [Display(Name = "Naam")]
        public string Name { get; set; }
        [Required]
        [Display(Name = "Adres")]
        public string Address { get; set; }
        [Required]
        [Display(Name = "Email")]
        public string Email { get; set; }
        [Required]
        [Display(Name = "Post Code")]
        public string Postal_code { get; set; }

    }

以及正在发布的方法

        public ViewResult StepThree(BookingViewModel model)
        {
            model.step = "PartialThree";
            return View("Book", model);
        }
马多克斯开发

尝试将其更改为属性而不是字段:

public List<PersonViewModel> persons {get;set;}

并从构造函数初始化它:

persons = new List<PersonViewModel>();

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

提交后模型为空

来自分类Dev

发布后,模型中的对象为空

来自分类Dev

从表单提交的空模型

来自分类Dev

MVC 4模型中的“提交表单”在控制器发布方法中为空

来自分类Dev

MVC 4模型中的“提交表单”在控制器发布方法中为空

来自分类Dev

MVC 5模型中的“提交表单”在控制器发布方法中为空

来自分类Dev

从Partial View向Controller提交表单时模型为空

来自分类Dev

提交后,Symfony2表单始终为空

来自分类Dev

提交后,Symfony2表单始终为空

来自分类Dev

为什么我在 Spring 提交表单后得到空对象?

来自分类Dev

表单提交获取空模型

来自分类Dev

表单提交后保持模型

来自分类Dev

在For Loop中具有表单的MVC视图提交空模型

来自分类Dev

提交表单后的空modelAttribute

来自分类Dev

Drupal 7表单在迁移后不起作用。提交后,表单字段为空

来自分类Dev

防止输入为空时提交(不在表单中)

来自分类Dev

如果表单为空,则阻止提交表单

来自分类Dev

提交表单后保留下拉列表中的值

来自分类Dev

如何使列表中的每个对象为空

来自分类Dev

为什么 GET 提交表单后 $_GET 为空,为什么是“?” 在表单提交时附加到 URL

来自分类Dev

表单提交后更新另一个模型的对象

来自分类Dev

手动提交Symfony表单后,CollectionType字段显示为空

来自分类Dev

在CakePHP 2.x中提交表单后,$ this-> request-> data为空

来自分类Dev

angularjs表单提交数据为空

来自分类Dev

模型中对象的 MVC 属性在发布时为空

来自分类Dev

提交表单后刷新评论列表

来自分类Dev

表单提交后如何获取新对象?

来自分类Dev

提交模型驱动的表单后如何清除表单?(角度2)

来自分类Dev

提交表单后如何显示加载图标,但如果模型在ASP .NET MVC中无效,则如何显示

Related 相关文章

  1. 1

    提交后模型为空

  2. 2

    发布后,模型中的对象为空

  3. 3

    从表单提交的空模型

  4. 4

    MVC 4模型中的“提交表单”在控制器发布方法中为空

  5. 5

    MVC 4模型中的“提交表单”在控制器发布方法中为空

  6. 6

    MVC 5模型中的“提交表单”在控制器发布方法中为空

  7. 7

    从Partial View向Controller提交表单时模型为空

  8. 8

    提交后,Symfony2表单始终为空

  9. 9

    提交后,Symfony2表单始终为空

  10. 10

    为什么我在 Spring 提交表单后得到空对象?

  11. 11

    表单提交获取空模型

  12. 12

    表单提交后保持模型

  13. 13

    在For Loop中具有表单的MVC视图提交空模型

  14. 14

    提交表单后的空modelAttribute

  15. 15

    Drupal 7表单在迁移后不起作用。提交后,表单字段为空

  16. 16

    防止输入为空时提交(不在表单中)

  17. 17

    如果表单为空,则阻止提交表单

  18. 18

    提交表单后保留下拉列表中的值

  19. 19

    如何使列表中的每个对象为空

  20. 20

    为什么 GET 提交表单后 $_GET 为空,为什么是“?” 在表单提交时附加到 URL

  21. 21

    表单提交后更新另一个模型的对象

  22. 22

    手动提交Symfony表单后,CollectionType字段显示为空

  23. 23

    在CakePHP 2.x中提交表单后,$ this-> request-> data为空

  24. 24

    angularjs表单提交数据为空

  25. 25

    模型中对象的 MVC 属性在发布时为空

  26. 26

    提交表单后刷新评论列表

  27. 27

    表单提交后如何获取新对象?

  28. 28

    提交模型驱动的表单后如何清除表单?(角度2)

  29. 29

    提交表单后如何显示加载图标,但如果模型在ASP .NET MVC中无效,则如何显示

热门标签

归档