当DropDownList从模型

时间:2018-04-28 14:56:27

标签: asp.net-mvc

我有一个在我的模型中返回的用户配置文件列表。 DropDownList中返回用户配置文件的名称。当我在DropDownList中选择一个名称时,我需要使用相关值填充TextBoxFor。

我的模特是;

public class Profiles
{
    public List<ProfileList> profiles { get; set; }
}

public class ProfileList
{
    public string id { get; set; }
    public string title { get; set; }
    public string firstName { get; set; } 
    public string surname { get; set; }
    public DateTime dateOfBirth { get; set; }
}

我的代码段是;

<div class="row init-row">
     <div class="col">
          <div class="label-count">
              @{ var paxNo = i + 1;}
              @Html.Label("PASSENGER #", "PASSENGER #" + paxNo.ToString())
          </div>
          @{
             List<SelectListItem> otherPax = new List<SelectListItem>();
             otherPax.Add(new SelectListItem { Text = "SELECT A PROFILE", Value = "0" });
             foreach (var pax in Model.profiles)
             {
                otherPax.Add(new SelectListItem { Text = pax.firstName + " " + pax.surname, Value = idString });}
                @Html.DropDownList("otherProfile", otherPax)
             } 
              </div>
           </div>
           <div class="row">
               <div class="col">
                  @Html.HiddenFor(model => model.passengers.otherPassengers[i].Id)
                  @Html.DropDownListFor(model => model.passengers.otherPassengers[i].title, Titles.TitleListItems(), "TITLE")
               </div>
        </div>
           <div class="row">
              <div class="col">
                   <div class="col-2">
                        <div class="input-wrap">
                             @Html.TextBoxFor(model => Model.passengers.otherPassengers[i].firstName, new { id="firstName", placeholder = "First name" })
                        </div>
                      </div>
                   </div>
           </div>
                   <div class="col">
                       <div class="input-wrap">
                           @Html.TextBoxFor(model => model.passengers.otherPassengers[i].lastName, new { placeholder = "Last name" })
                       </div>
                   </div>
                <div class="row">
                   <div class="col">
                      @Html.TextBoxFor(model => model.passengers.otherPassengers[i].dateOfBirth, "{0:ddMMyyyy}", new { @class = "date", id = "dateOfBirthOtherPax", placeholder = "DATE OF BIRTH" })
                   </div>
                </div>
</div>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

通过在onchange处理程序上使用多个事件来实现它的工作