Предотвратите дальнейшее зацикливание и добавьте оставшиеся поля в зависимости от количества в MVC 5 Razor - PullRequest
0 голосов
/ 16 мая 2018

Привет, у меня просто проблема в MVC 5, где мне нужно передать значение в первом цикле и оставить его пустым в оставшихся циклах, в зависимости от того, сколько еще осталось отсчетов (извините, что не могу объяснить это в технической термины)

Вот мой набор кодов:

В моей модели:

public class EventAttendeeViewModel
    {
 public IList<getAttendeeList> getAttendeeLists {get;set;}
    }
    public class getAttendeeList
    {
        public virtual int? AttendeeTypeId { get; set; }

        [Required(ErrorMessage = "Last Name is required")]
        [StringLength(50, ErrorMessage = "Last Name cannot be longer than 50 characters.")]
        [Display(Name = "Last Name", Prompt = "Attendee Last Name")]
        [DataType(DataType.Text)]
        public string LastName { get; set; }


        [Required(ErrorMessage = "First Name is required")]
        [StringLength(50, ErrorMessage = "First Name cannot be longer than 50 characters.")]
        [Display(Name = "First Name", Prompt = "Attendee First Name")]
        [DataType(DataType.Text)]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "PhoneNumber is required")]
        [Display(Name = "Cell Phone")]
        [DataType(DataType.PhoneNumber)]
        public string CellPhone { get; set; }
}

в моем контроллере

eventattendee.AdultCount = model.AdultCount;
eventattendee.AttendeeCount = model.eventattendee();
var listOfAttndees = new List<getAttendeeList>();
                var lstAttendees = new getAttendeeList();
                lstAttendees .FirstName = userInfo.FirstName;
                lstAttendees .LastName = userInfo.LastName;  
                lstAttendees .CellPhone = Helpers.CommonUtils.RemovePhoneFormat(phoneDetail.PhoneNumber);
                lstAttendees .Email = userInfo.EmailAddress;
                listOfAttndees .Add(lstAttendees );

                eventattendee.getAttendeeLists = listOfAttndees;

return View (eventAttendee);

и на мой взгляд

 @if (Model.AttendeeCount > 0 && Model != null)
                            {

                                for (int i = 0; i < Model.AdultCount; i++)
                                {

                                    @Html.HiddenFor(m=>m.isAdult)
                                    <div id="div_@i">
                                        <h2>Adult @(i + 1) </h2>
                                        <div class="form-group">
                                            @Html.LabelFor(model => model.getAttendeeLists[i].FirstName, new { @class = "control-label col-md-2" })
                                            <div class="col-md-6">
                                                @Html.EditorFor(model => model.getAttendeeLists[i].FirstName, new { htmlAttributes = new { @class = "form-control" } })
                                                @Html.ValidationMessageFor(model => model.getAttendeeLists[i].FirstName, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            @Html.LabelFor(model => model.getAttendeeLists[i].LastName, new { @class = "control-label col-md-2" })
                                            <div class="col-md-6">
                                                @Html.EditorFor(model => model.getAttendeeLists[i].LastName, new { htmlAttributes = new { @class = "form-control" } })
                                                @Html.ValidationMessageFor(model => model.getAttendeeLists[i].LastName, "", new { @class = "text-danger" })
                                            </div>

                                        </div>
                                        <div class="form-group">
                                            @Html.LabelFor(model => model.getAttendeeLists[i].CellPhone, htmlAttributes: new { @class = "control-label col-md-2" })
                                            <div class="col-md-6">
                                                <div class="input-group">
                                                    <span class="input-group-addon">
                                                        <i class="glyphicon glyphicon-phone"></i>
                                                    </span>
                                                    @Html.TextBoxFor(model => model.getAttendeeLists[i].CellPhone, new { @class = "form-control PhoneMask" })
                                                </div>
                                                @Html.ValidationMessageFor(model => model.getAttendeeLists[i].CellPhone, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            @Html.LabelFor(model => model.getAttendeeLists[i].Email, htmlAttributes: new { @class = "control-label col-md-2" })
                                            <div class="col-md-6">
                                                @Html.EditorFor(model => model.getAttendeeLists[i].Email, new { htmlAttributes = new { @class = "form-control" } })
                                                @Html.ValidationMessageFor(model => model.getAttendeeLists[i].Email, "", new { @class = "text-danger" })
                                            </div>
                                        </div>
                                    </div>

                                }

Каждый раз, когда я отлаживаю, он запускает System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index в цикле Second for Model.AdultCount

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...