2011-09-30 5 views
0

Мне нужно иметь несколько так называемых «создавать» формы на мой взгляд. Мне нужен совет относительно того, как это сделать. Моя форма в основном позволяет пользователю создавать пассажирские реквизиты/книги для нескольких пассажиров в зависимости от числа, которое они выбрали из выпадающего списка. Используя javascript, больше div будет отображаться, чтобы удовлетворить их выбор, максимум до 3. Как бы я хотел создать эти пассажиры на моей модели с помощью одной кнопки отправки? Как мне собрать эти ценности? Есть в общей сложности 3 дивы, и более пассажирских деталей пользователь хочет забронировать для, тем больше дивы являются shown.My вид в настоящее время выглядит следующим образом:Как создать «Создать представление» с несколькими динамическими полями?

<h2>Booking</h2> 
<div class="editor-label"> 
    <%=Html.Label("Select number of passengers") %> 
</div> 
<div class="editor-field"> 
    <%=Html.DropDownList("PassengerNumber", new List<SelectListItem> 
       { 
        new SelectListItem{Text="1", Value="1"}, 
        new SelectListItem{Text="2", Value="2"}, 
        new SelectListItem{Text="3", Value="3"} 
        })%> 
</div> 
<% using (Html.BeginForm()) {%> 
    <%= Html.ValidationSummary(true) %> 

    <fieldset> 
     <legend>Fields</legend> 

     <div id="div1"> 
     <h2>Passenger Details 1</h2> 
     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.flight_number) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.flight_number, ViewData["Flight_Number"]) %> 
      <%= Html.ValidationMessageFor(model => model.flight_number) %> 
     </div> 



     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.title) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.title) %> 
      <%= Html.ValidationMessageFor(model => model.title) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.first_name) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.first_name) %> 
      <%= Html.ValidationMessageFor(model => model.first_name) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.last_name) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.last_name) %> 
      <%= Html.ValidationMessageFor(model => model.last_name) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.date_of_birth) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.date_of_birth) %> 
      <%= Html.ValidationMessageFor(model => model.date_of_birth) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.passport_number) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.passport_number) %> 
      <%= Html.ValidationMessageFor(model => model.passport_number) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.address) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.address) %> 
      <%= Html.ValidationMessageFor(model => model.address) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.phone) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.phone) %> 
      <%= Html.ValidationMessageFor(model => model.phone) %> 
     </div> 
     </div> 

     <br /> 
     <div id="div2"> 
     <h2>Passenger Details 2</h2> 
     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.flight_number) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.flight_number, ViewData["Flight_Number"]) %> 
      <%= Html.ValidationMessageFor(model => model.flight_number) %> 
     </div> 



     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.title) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.title) %> 
      <%= Html.ValidationMessageFor(model => model.title) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.first_name) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.first_name) %> 
      <%= Html.ValidationMessageFor(model => model.first_name) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.last_name) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.last_name) %> 
      <%= Html.ValidationMessageFor(model => model.last_name) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.date_of_birth) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.date_of_birth) %> 
      <%= Html.ValidationMessageFor(model => model.date_of_birth) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.passport_number) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.passport_number) %> 
      <%= Html.ValidationMessageFor(model => model.passport_number) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.address) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.address) %> 
      <%= Html.ValidationMessageFor(model => model.address) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.phone) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.phone) %> 
      <%= Html.ValidationMessageFor(model => model.phone) %> 
     </div> 
     </div> 
     <br /> 
     <div id="div3"> 
     <h2>Passenger Details 3</h2> 
     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.flight_number) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.flight_number, ViewData["Flight_Number"]) %> 
      <%= Html.ValidationMessageFor(model => model.flight_number) %> 
     </div> 



     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.title) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.title) %> 
      <%= Html.ValidationMessageFor(model => model.title) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.first_name) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.first_name) %> 
      <%= Html.ValidationMessageFor(model => model.first_name) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.last_name) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.last_name) %> 
      <%= Html.ValidationMessageFor(model => model.last_name) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.date_of_birth) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.date_of_birth) %> 
      <%= Html.ValidationMessageFor(model => model.date_of_birth) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.passport_number) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.passport_number) %> 
      <%= Html.ValidationMessageFor(model => model.passport_number) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.address) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.address) %> 
      <%= Html.ValidationMessageFor(model => model.address) %> 
     </div> 

     <div class="editor-label"> 
      <%= Html.LabelFor(model => model.phone) %> 
     </div> 
     <div class="editor-field"> 
      <%= Html.TextBoxFor(model => model.phone) %> 
      <%= Html.ValidationMessageFor(model => model.phone) %> 
     </div> 
     </div> 

     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 

<% } %> 

<div> 
    <%= Html.ActionLink("Back to List", "Index") %> 
</div> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#div2").hide(); 
      $("#div3").hide(); 
     }); 
     $("#PassengerNumber").change(function() { 
      if ($("#PassengerNumber").val() == "1") { 
       $("#div2").hide(); 
       $("#div3").hide(); 
      } 
      if ($("#PassengerNumber").val() == "2") { 
       $("#div2").show(); 
       $("#div3").hide(); 
      } 
      if ($("#PassengerNumber").val() == "3") { 
       $("#div2").show(); 
       $("#div3").show(); 
      } 
     }); 
</script> 

ответ

1

Ваш код указывает на то, что ваша модель представляет собой один экземпляр - вместо этого вам нужно выбрать модель, представляющую собой набор экземпляров. Так что ваша наценка будет что-то вроде

<% for (int i = 0; i < 3; i++) { %> 

    <div id="div1"> 
     <h2>Passenger Details <%: i %></h2> 
     <div class="editor-label"> 
      <%= Html.LabelFor(model => model[i].flight_number) %> 
     </div> 
... 

ASP.NET MVC does support model binding to a list/collection.

this blog post См где автор подробно остановился (вместе с демо-проекта), как редактировать список переменной длины, и дать ссылку, чтобы добавить еще один пункт и т.д.

Смежные вопросы