Проверка не запускается в Bootstrap Modal - PullRequest
0 голосов
/ 02 апреля 2019

Я пытаюсь переместить некоторые из наших существующих представлений создания в модалы Bootstrap с помощью удаленных вызовов содержимого, однако существующая проверка, настроенная для полей (т. Е. Имя региона), не запускается, когда я нажимаю кнопку Отправить, как только онавводится в Модал.

Я хотел бы знать, что я делаю неправильно, и если есть лучший способ попытаться вытащить существующий div с другой страницы в Модал с проверкой.

Index.cshtml

@Html.ActionLink("Create New Region", "Create", null, new { @class = "btn", onclick = "cselect(this)", title = "Create New Region", data_toggle = "modal", data_target = "#AdminModal", data_remote = "false" })

Layout.cshtml

    <!-- Modal HTML -->
    <div class="modal fade" id="AdminModal" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="ModalLabel"></h4>
                </div>
                <div class="modal-body">
                </div>
            </div>
        </div>
    </div>
    <script>
        // Modal JS
        function cselect(obj) {
            ActionLinkTitle = $(obj).attr("title");
        }

        $("#AdminModal").on("show.bs.modal", function (e) {
            var link = $(e.relatedTarget);
            $(this).find(".modal-body").load(link.attr("href") + ' #modal');
            $(this).find(".modal-title").html(ActionLinkTitle);

        });
    </script>

Create.cshtml - JavaScript извлекает содержимое из div id = "modal" и вставляет его вМодально вместо того, чтобы перетаскивать содержимое всей страницы.

@using System.Collections.Generic
@model Website.ViewModel.CountryViewModel
@{
    ViewBag.Title = "Create New Region";
}

<div style="padding: 20px 20px 20px 20px; font-size: 8pt;">
    Home > Regions >
    <strong>@ViewBag.Title</strong>
</div>

<div id="modal" style="padding: 0 20px 20px 20px;">

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>Region Name</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.Name)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Name)
                @Html.ValidationMessageFor(model => model.Name)
            </div>
            <br />
                <div class="editor-label">
                    @Html.LabelFor(model => model.ServerId, "Server Alias")
                </div>
                <div class="editor-field">
                    @Html.DropDownListFor(model => model.ServerId, (IEnumerable<SelectListItem>)ViewBag.ServerId)
                    @Html.ValidationMessageFor(model => model.ServerId)
                </div>
                <br />
            <p />
            <p>
                <input type="submit" value="Create" class="btn" />
            </p>
        </fieldset>
    }
</div>

<div style="padding: 0 20px 20px 20px;">
    @Html.ActionLink("Back to Regions", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

CountryViewModel.cs - Обязательный параметр указывается в имени региона.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace Website.ViewModel
{
    public class CountryViewModel
    {
        public CountryViewModel()
        {
            CustomFieldDefinitions = new List<EntityCustomFieldDefinitionViewModel>();
        }

        public int Id { get; set; }

        [Required]
        [StringLength(256, MinimumLength = 1)]
        [Display(Name = "Region Name")]
        public string Name { get; set; }

        [Required]
        [Display(Name = "Server")]
        public int ServerId { get; set; }

        [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy HH:mm:ss '(UTC)'}")]
        public DateTime? RetDate { get; set; }

        public string ServerAlias { get; set; }

        [Display(Name = "Customer Fields")]
        public IEnumerable<EntityCustomFieldDefinitionViewModel> CustomFieldDefinitions { get; set; }
    }
}

Спасибо.

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