【问题标题】:Pass a List from HTML Razor View via Form通过表单从 HTML Razor 视图传递列表
【发布时间】:2018-07-17 18:03:44
【问题描述】:

这是我的 Tennant 班级,其中有联系人列表

public class Tennant
    {
        public Tennant()
        {
            ContactPerson = new List<HR.ContactPerson>();
        }
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public string BrandName { get; set; }
        public string OfficeAddress { get; set; }

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

    }

这是联系人类

  public class ContactPerson
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public long CNIC { get; set; }
        public DateTime CNICExpiry { get; set; }
        public long MobileNumber { get; set; }
        public string Email { get; set; }
        public Address Address { get; set; }
    }

我无法理解如何为列表创建视图(表单)

 <div class="form-group">
                @Html.LabelFor(x => x.CompanyName, new { })
                @Html.TextBoxFor(x => x.CompanyName, new { @class = "form-control", id = "CompanyName", placeholder = "Enter Company Name", type = "text" })
                <small id="companyName" class="form-text text-muted">Use M/S first.</small>
            </div>
            <div class="form-group">
                @Html.LabelFor(x => x.BrandName)
                @Html.TextBoxFor(x => x.BrandName, new { @class = "form-control", id = "BrandName", placeholder = "Enter Brand Name", type = "text" })
                <small id="brandName" class="form-text text-muted">Full Product / Brand Name</small>
            </div>
            <div class="form-group">
                @Html.LabelFor(x => x.OfficeAddress)
                @Html.TextBoxFor(x => x.OfficeAddress, new { @class = "form-control", id = "OfficeAddress", placeholder = "Enter Office Address", type = "text" })
                <small id="officeAddress" class="form-text text-muted">Full Office Address.</small>
            </div>

无法理解如何管理联系人列表

 @Html.TextBoxFor(x=>x.ContactPerson.)

我该怎么办?

提前感谢。

【问题讨论】:

    标签: c# html asp.net-mvc razor model-view-controller


    【解决方案1】:

    您需要使用for-loop,然后默认的模型绑定器才能工作:

    @for (int i = 0; i < Model.ContactPerson.Count; ++i)
    {
        @Html.TextBoxFor(x => x.ContactPerson[i].FirstName)
    }
    

    注意:您需要在列表中添加一个“空”ContactPerson 实例以获取新人的输入字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多