Modal中的AddModelError

时间:2016-02-23 00:08:39

标签: jquery asp.net asp.net-mvc twitter-bootstrap asp.net-mvc-4

我遇到了与这些人相同的问题,postback(AddModelError)出现在没有css的主屏幕中,而不是在模态弹出窗口中,这里有问题......

Twitter Bootstrap modal does not work properly when implementing in asp.net mvc

MVC 4: Return partial view inside of bootstrap modal upon failed validation

我已尝试按照与所述解决方案相同的步骤进行操作,但回发仍在主屏幕上(索引)

我没有看到什么是错的,我想知道我的代码有什么问题。

谢谢。

_Registration PartialView

<div id="ModalForm">
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="myModalLabel-Update">Agregar Agencia</h4>
</div>

@using (@Html.BeginForm("Registration", "Agency", FormMethod.Post, new { @class = "form-horizontal", @id = "registrationSave" }))
{
    <div class="modal-body">           
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <fieldset>
            <div class="form-group-sm col-sm-6" style="padding-top:7px;">
                @Html.Label("Direccion de Correo Electronico")
                <div class="input-group">
                    <span class="input-group-addon">
                        <i class="ace-icon fa fa-envelope"></i>
                    </span>
                    @Html.TextBoxFor(u => u.Email, new { @class = "form-control", @placeholder = "Correo Electronico" })
                </div>
                @Html.ValidationMessageFor(u => u.Email)
            </div>

            <div class="form-group-sm col-sm-6">
                @Html.Label("Nombre de Agencia")
                @Html.TextBoxFor(u => u.NameAgency, new { @class = "form-control", @placeholder = "Nombre de Agencia" })
                @Html.ValidationMessageFor(u => u.NameAgency)
            </div>

            <div class="form-group-sm col-sm-6" style="padding-top:7px;">
                @Html.Label("# de Contrato - Ejemplo: 5784")
                @Html.TextBoxFor(u => u.NContract, new { @class = "form-control limited", @placeholder = "Numero Contrato", @maxlength = "4" })
                @Html.ValidationMessageFor(u => u.NContract)
            </div>

            <div class="form-group-sm col-sm-6" style="padding-top:7px;">
                @Html.Label("Telefono Contacto (999)-999-9999")
                <div class="input-group">
                    <span class="input-group-addon">
                        <i class="ace-icon fa fa-phone"></i>
                    </span>
                    @Html.TextBoxFor(u => u.Telefono, new { @class = "form-control", @placeholder = "Telefono Contacto" })
                </div>
                @Html.ValidationMessageFor(u => u.Telefono)
            </div>            
        </fieldset>
    </div>
    <div class="modal-footer">
        <button class="btn btn-sm btn-primary" id="postSave">Guardar</button>
        <button type="button" class="btn btn-sm btn-defaultt" data-dismiss="modal">Cerrar</button>
    </div>
}

Index.cshtml

@{
ViewBag.Title = "Agencias";
}

<div id="agencyList">
    @{Html.RenderAction("RenderList", "Agency");}
</div>

<!--Modal Charge-->
<div class="modal fade" id="Modal-Charge" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="msgError"></div>
        <div class="modal-content" id="ModalContent">

        </div>
    </div>
</div>

<!-- inline scripts related to this page -->
<script type="text/javascript">
    jQuery(function ($) {
        $('a.newRegister').click(function (e) {
            e.preventDefault();
            var url = '@Url.Action("Registration", "Agency")';
            GetModal(url);
        });

        function GetModal(url) {
            var form = $("#Modal-Charge form");
            $('#ModalContent').load(url, function (html) {
                $("#Modal-Charge").modal('show');
            });
        }

        $('#postSave').click(function (e) {
            e.preventDefault()
            $.ajax({
                url: '@Url.Action("Registration","Agency")',
                type: 'POST',
                dataType: 'html',
                data: $('#registrationSave').serialize(),
                success: function (result) {
                    if (result.success) {
                        $('#Model-Charge').modal('hide');
                    }
                    else {
                        $('#ModalContent').html(result);
                    }
                }
            });
        });       


        $("a.newRegister, a.editAgency, a.deleteAgency").tooltip({
            show: {
                effect: "slideDown",
                delay: 250
            }
        });
    })
</script>

控制器

    public ActionResult Registration()
    {
        ViewBag.CountryID = new SelectList(db.Country, "CountryID", "CountryDesc", "Seleccione un País");
        ViewBag.StateID = new SelectList(db.State, "StateID", "StateDesc");

        return PartialView("_Registration");
    }

    [HttpPost]
    public ActionResult Registration(CheckRegistration user)
    {
        ViewBag.CountryID = new SelectList(db.Country, "CountryID", "CountryDesc", user.CountryID);
        ViewBag.StateID = new SelectList(db.State, "StateID", "StateDesc", user.StateID);

        var checkEmail = db.SystemUser.AsNoTracking().Where(u => u.Email == user.Email.Trim()).FirstOrDefault();
        if (checkEmail != null)
        {
            ModelState.AddModelError("Email", "Correo Electronico ya se encuentra registrado, Verifique");
            return PartialView("_Registration", user);
        }
        else
        {
            return Json(new { success = true });  
        }            
    }

1 个答案:

答案 0 :(得分:0)

我按照解决方案提供给Stephen Muecke Remote验证

模特

[Required]
[DataType(DataType.EmailAddress)]
[Remote("isEmail_Available", "Agency")]
[StringLength(150)]
[Display(Name = "Correo Electronico")]
public string Email { get; set; }

控制器

public JsonResult isEmail_Available(string Email)
{
    var checkEmail = db.SystemUser.AsNoTracking().Where(u => u.Email == Email).FirstOrDefault();
    if (checkEmail == null)
        return Json(true, JsonRequestBehavior.AllowGet);

    string suggestedEmail = String.Format("{0} ya registrado.", Email);

    return Json(suggestedEmail, JsonRequestBehavior.AllowGet);
}