简体   繁体   English

MVC 3:在_Layout视图上更改用户jQueryUi对话框,如何处理?

[英]MVC 3: Change User jQueryUi dialog on _Layout view, how to handle?

I am trying to implement an admin feature on my site that allows an admin to impersonate a user by inputting their username (to see what they see). 我正在尝试在我的网站上实现管理员功能,允许管理员通过输入用户名来模仿用户(以查看他们看到的内容)。

Right now I have this setup as a jQueryUi dialog in the _LogOn partial view in the _Layout master view since this will be on every page. 现在,我在_Layout主视图的_LogOn部分视图中将此设置作为jQueryUi对话框,因为它将出现在每个页面上。 However, I am not sure what to do at this point due to some issues I've never dealt with before. 但是,由于某些我从未处理过的问题,我目前不确定该怎么办。

1) I need to run a check to see if the inputted username exists, if it doesn't it should display a message in the dialog's form. 1)我需要运行检查以查看输入的用户名是否存在,如果不存在,则应在对话框的表单中显示一条消息。

2) I need to reload the entire page (so what is being displayed changes due to the new user updates) after submitting. 2)提交后,我需要重新加载整个页面(由于新用户更新,正在显示的内容发生了变化)。

3) The box should appear open again after the page reloads. 3)重新加载页面后,该框应再次显示为打开。 After the dialog form is submitted. 对话框提交后。

So I don't know how to implement this because partials have no controllers, nor do I know how to make a jquery box reappear based on if the submit came from that specific form nor how to get an error message up when the partial isn't based on a model. 所以我不知道如何实现这一点,因为partials没有控制器,也不知道如何基于提交的表单是否来自特定表单来重新显示jQuery框,或者当partial不是时如何获取错误消息。 t基于模型。 Then I thought about making the form AJAX, but I am not sure how to do that either since the entire page eventually has to be reloaded to update the display. 然后我考虑制作AJAX表单,但是我也不知道该怎么做,因为最终必须重新加载整个页面以更新显示。

The addition to _LogOn partial besides the default code: _LogOn部分除了默认代码外:

if (SessionUtil.IsSiteAdmin)
{
    <button id="impersonate" style="position: relative; right: 20px; float: left; top: -30px;">Impersonate</button>
    <div id="impersonate-user" title="Admin User Impersonation">
        @using (Html.BeginForm())
        {
            <p>Currently Impersonating: 
                @if (SessionUtil.User.Id != SessionUtil.UserAbsolute.Id)
                {
                    <text>
                    @SessionUtil.User.FirstName @SessionUtil.User.LastName (@SessionUtil.User.NetId)
                    </text>
                }
                else
                {
                    <text>None</text>
                }
            </p>
            <p>@Html.ActionLink("Stop Impersonating", "StopImpersonating", "Shared", new { area = "", @class = "ui-button ui-widget ui-state-default ui-corner-all" })</p>
            <div>
                <div id="ValidationSummary">
                @Html.ValidationSummary(true)
                </div>
                <fieldset>
                    <div class="editor-label">
                        @Html.Label("Username to Impersonate")
                    </div>
                    <div class="editor-field">
                        @Html.TextBox("Username")
                    </div>

                    <p>
                        <input type="submit" value="Impersonate"/>
                    </p>
                </fieldset>
            </div>
        }
    </div>
}

The JS: JS:

 <script type="text/javascript">
        $("#impersonate-user").dialog({
            autoOpen: false,
            modal: false,
            width: 400,
            height: 505,
            closeOnEscape: true,
            resizable: false,
            draggable: true
        });
        $("#impersonate")
            .button()
            .click(function () {
                $("#impersonate-user").dialog("open");
                $('.ui-dialog :button, .ui-dialog a').blur();
            });
    </script>

Can someone tell me how to do this? 有人可以告诉我该怎么做吗? Thanks. 谢谢。

I would recommend using ajax to do the name lookup and show an error message if it's not found. 我建议使用ajax进行名称查找,如果找不到,则显示错误消息。 No need to reload the page for a failure. 无需为失败而重新加载页面。

It looks like you're using jQuery, so for #1 and #2: 看起来您正在使用jQuery,因此对于#1和#2:

$.ajax({
     url: actionUrl,
     data: { username: username },
     success: function() {
         window.location = window.location // I think this will refresh the page
     },
     error: function(error) {
         alert('username not found');  // do what you have to do
     }
});

What I would do for your case is have an action that you call with the above Ajax. 对于您的情况,我要做的就是使用上述Ajax调用一个操作。 If the username exists, it sets some flags in session, eg: Session["IsImpersonating"] = true and then you could have a block in your page that checks the session, and shows the dialog again: 如果用户名存在,则会在会话中设置一些标志,例如: Session["IsImpersonating"] = true ,然后页面中可能会有一个块检查会话,并再次显示对话框:

@if ((bool)Session["IsImpersonating"]) // null check omitted
{
     <script>
           $(function() {
                 $("#myDialog").dialog();
           });
     </script>
}

FYI, to run an action with a partial, I think you just have to do the following: 仅供参考,要部分执行某项操作,我认为您只需执行以下操作:

@Html.Action("MyAction", "MyController")

This will make a call back to the server, just as if you did a regular page load, and plop the HTML for whatever view it returns onto the page. 就像您进行常规页面加载一样,这将调用服务器,并为返回页面的任何视图放置HTML。

Hope this helps. 希望这可以帮助。

I would suggest implementing a custom IIdentity, that can replace the current user id when you want to impersonate someone. 我建议实现一个自定义的IIdentity,当您要模拟某人时可以替换当前的用户ID。 It would return the normal user when User.Current.Name is called, or the impersonated user when User.Current.Name is used. 调用User.Current.Name时,它将返回普通用户;使用User.Current.Name时,将返回模拟用户。

This is a bit more work on the front side, but then you never have to worry about it in any of your pages.. it just works like normal. 这在前端需要做更多的工作,但是您不必在任何页面中都为它担心。它就像正常一样工作。 In addition, you won't have to change any of your logic in the main app. 此外,您无需在主应用程序中更改任何逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM