【问题标题】:Bootstrap modal dialog return message in MVCMVC中的引导模式对话框返回消息
【发布时间】:2016-07-19 15:46:00
【问题描述】:

我需要一些帮助来从 mvc 的部分视图中返回引导模式对话框中的成功或错误消息。

当点击 _LayoutAdmin 视图中的链接时,部分视图 Impersonation.cshtml 将加载到模态对话框中。

输入并单击提交按钮后,弹出框将关闭并在控制器中发布。如果用户输入数据不存在,如何在模态对话框中显示错误消息?

非常感谢任何指导!

截图:

_LayoutAdmin.cshtml:

<a href="@Url.Action("Impersonation", "UserRoles")" class="modal-link">
  Impersonation &nbsp;
   <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
</a>

<div id="modal-container" class="modal fade" tabindex="-1" role="dialog">
</div>

<script>

 $(function () {

                $('body').on('click', '.modal-link', function (e) {
                    e.preventDefault();
                    $(this).attr('data-target', '#modal-container');
                    $(this).attr('data-toggle', 'modal');
                });
                // Attach listener to .modal-close-btn's so that when the button is pressed the modal dialog disappears
                $('body').on('click', '.modal-close-btn', function () {
                    $('#modal-container').modal('hide');
                });
                //clear modal cache, so that new content can be loaded
                $('#modal-container').on('hidden.bs.modal', function () {
                    $(this).removeData('bs.modal');
                });
                $('#CancelModal').on('click', function () {
                    return false;
                });
        });

    </script>

(部分视图)Impersonation.csthml:

@model FAB_Portal.Models.FAB_View
@using (Html.BeginForm())
{ 
    <script type="text/javascript">

        $("a#impersonate").click(function () {
            var username = $('#UserName').val();
            $.ajax({
                type: "POST",
                url: "@Url.Action("Impersonation", "UserRoles")",
                data: { UserName: username },
                success: function (result) {
                    if (result.success) {
                        $('#modal-dialog').modal('hide');

                    } else {
                        $('#modal-body').html(result);
                    }
                }
            });
        });

    </script>

<div class="modal-dialog">
    <div class="modal-content">

        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel"><span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;Impersonation</h4>
        </div>

        <div class="modal-body">
            <div class="form-horizontal">
                @Html.Label("Impersonate", htmlAttributes: new { @class = "control-label col-md-4" })
                @Html.TextBox("UserName", null, htmlAttributes: new { @class = "form-control" })
                <text class="text-danger">@ViewBag.Error</text>
            </div>
        </div>

        <div class="modal-footer">
            @using (Html.BeginForm("Impersonation", "UserRoles", FormMethod.Post))
            {
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <a class="btn btn-danger btn-ok" id="impersonate" >Submit</a>
            }
        </div>
    </div>
</div>
}

UserRoles.Controller:

public ActionResult Impersonation()
{
    return PartialView("Impersonation");
}

[HttpPost]
public ActionResult Impersonation(FAB_View model)
{
    if (ModelState.IsValid)
    {
        UserRoleHelper userrolehelper = new UserRoleHelper();
        bool validuser = userrolehelper.CheckValidUser(model.UserName);
        if (validuser == false)
        {
            ViewBag.Error = "Don't have such user!";
            return PartialView("Impersonation");
        }
        userrolehelper.StartImpersonate(model.UserName);
    }        
    return PartialView("Impersonation");
}

【问题讨论】:

  • 您的意思是要显示"Don't have such user!" 消息?
  • 没错,否则如果用户名提交成功,它将显示“成功”提交。没关系,我稍后可以为它添加另一个 viewbag,现在我只是不确定如何将消息传递回模态对话框。
  • 只需在您的局部视图中添加&lt;div&gt;@ViewBag.Error&lt;/div&gt;。但似乎不需要返回视图 - 您可以只返回一个指示成功或失败的值,然后如果失败,则显示一条消息
  • 我在局部视图中添加了 viewbag 错误。但是当我单击提交时,模式对话框将直接关闭。除非成功提交,否则我不希望它关闭。我不确定如何使用模态对话框来实现它。
  • 那么你将如何显示"successfully submited" 消息?正如我所指出的,最好返回指示成功或其他情况的 json(以及错误消息)

标签: jquery ajax asp.net-mvc twitter-bootstrap modal-dialog


【解决方案1】:

使用以下方法自行修复:

LayoutAdmin 视图:

<script>
 $(function () {

        $.ajaxSetup({ cache: false });

        $("#impersonate").on("click", function (e) {

            // hide dropdown if any
            $(e.target).closest('.btn-group').children('.dropdown-toggle').dropdown('toggle');

            $('#myModalContent').load(this.href, function () {
                $('#myModal').modal({
                    /*backdrop: 'static',*/
                    keyboard: true
                }, 'show');
                bindForm(this);
            });
            return false;
        });
    });

    function bindForm(dialog) {

        $('form', dialog).submit(function () {
            $.ajax({
                url: this.action,
                type: this.method,

                data: $(this).serialize(),
                success: function (result) {
                    if (result.success) {
                        $('#myModal').modal('hide');
                        //Refresh
                        location.reload();
                    } else {
                        $('#myModalContent').html(result);
                        bindForm();
                    }
                }
            });
            return false;
        });
    }

</script>

Impersonation.cshtml:

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Impersonation(FAB_View model)
        {
            if (ModelState.IsValid)
            {
                UserRoleHelper userrolehelper = new UserRoleHelper();
                var loggedonuser = userrolehelper.GetLoggedOnUser();
                var currentuser = userrolehelper.GetCurrentUser();

                bool validuser = userrolehelper.CheckValidUser(model.UserName);

                if (validuser == false)
                {
                    ViewBag.Error = "* Don't have such user!";
                    return PartialView("Impersonation", model);
                    //return Json(new { success = false });
                }
                else
                {
                    //userrolehelper.StartImpersonate(model.UserName);
                    return Json(new { success = true });
                }               
            }

            return PartialView("Impersonation", model);

        }

【讨论】:

    【解决方案2】:

    试试这个修改(部分视图)Impersonation.csthml:

      @model FAB_Portal.Models.FAB_View
      @using (Html.BeginForm("Impersonation", "UserRoles", FormMethod.Post,new { @id="form" }))
                    { 
    
        <div class="modal-dialog">
            <div class="modal-content">
    
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel"><span class="glyphicon glyphicon-user" aria-hidden="true"></span>&nbsp;Impersonation</h4>
                </div>
    
                <div class="modal-body">
                    <div class="form-horizontal">
                        @Html.Label("Impersonate", htmlAttributes: new { @class = "control-label col-md-4" })
                        @Html.TextBox("UserName", null, htmlAttributes: new { @class = "form-control" })
                        <text class="text-danger">@ViewBag.Error</text>
                    </div>
                </div>
    
                <div class="modal-footer">
    
                        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        <a class="btn btn-danger btn-ok" id="impersonate" data-dismiss="modal">Submit</a>
    
                </div>
            </div>
        </div>
        }
        <script type="text/javascript">
    
                $("a#impersonate").click(function () {
    
                    $.ajax({
                        type: "POST",
                        url: "@Url.Action("Impersonation", "UserRoles")",
                        data:   $('#form').serialize(),
                        success: function (result) {
                            if (result == "success") {
                                $('#dialogDiv').modal('hide');
    
                            } else {
                                $('#dialogContent').html(result);
                            }
                        }
                    });
                });
    
            </script>
    

    UserRoles.Controller:

       [HttpPost]
    public ActionResult Impersonation(FAB_View model)
    {
        if (ModelState.IsValid)
        {
            UserRoleHelper userrolehelper = new UserRoleHelper();
            bool validuser = userrolehelper.CheckValidUser(model.UserName);
            if (validuser == false)
            {
                ViewBag.Error = "Don't have such user!";
                return Content("error");
            }
            userrolehelper.StartImpersonate(model.UserName);
        }        
        return Content("success");
    }
    

    试试这个,如果它有效,不要忘记点击喜欢。 让我知道它是否无法提供另一种方式或修改这个方式。

    【讨论】:

    • 能否请您插入警报(结果);在成功函数中并回复您收到的响应
    • 或提供你的项目来修复它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2014-03-09
    • 2013-05-05
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多