JQuery Modal - 隐藏 Div 与部分视图 (MVC3)

发布于 2024-12-10 22:40:12 字数 859 浏览 0 评论 0原文

我正在使用 MVC3/C#/ASP.NET 创建一个网站,其中包含一个视图页面,该页面将显示 JQuery 模态类型弹出窗口(实现与传统模态窗口有点不同,但功能非常相似)。目前,我有将进入模态窗口的标记,存储为部分视图(具有单独的关联模型)。

我正在对 JQuery 模态窗口进行一些研究,我在网上看到的很多示例似乎都使用带有隐藏 div 的单个视图,并且模态窗口只是填充了隐藏 div 内部的标记。在这些示例中,只有一个视图(页面的主视图),隐藏 div 的输入字段(等)后面的模型数据仅存储在与主视图关联的模型中(而不是拥有单独的视图和我想要显示的标记的模型,如果我要使用部分视图实现方案,情况就会如此)。

我想知道是否有人可以提供一些关于首选方法或标准做法(如果有)的见解。这只是个人喜好问题吗?一种方法比另一种方法更有效吗?

我应该采用哪种方法:

  • 具有单独视图/模型的模式窗口标记的部分视图
  • < strong>现有视图/模型中的隐藏 Div

注意:我是开发团队的一员,因此保持事物与标准实践保持一致非常重要。

对此事的任何想法将不胜感激, 谢谢

编辑:我询问了参与同一项目的开发人员同事,他们是如何做类似的事情的。看起来无论我与他们如何争论这个问题,他们似乎都同意“最好的”和“最有效的方法”是渲染部分视图,每个视图都有一个单独的模型。我真的不明白为什么他们都认为这种方法比使用隐藏的 div 更有效,但由于我目前是“新人”,我没有过多地推动这个问题,基本上只是假装同意他们的观点试图提高效率。

TL;DR:无论我喜欢与否,我现在都在使用部分视图。不过,我仍然想听听更多对此的意见。

I am creating a website using MVC3/C#/ASP.NET which contains a view page that will show a JQuery Modal type popup (the implementation is a bit different than a traditional modal window, but the functionality is quite similar). Currently I have the markup that will go into the modal window stored as a partial view (with a separate associated model).

I was doing a little research on JQuery modal windows, and it seems a lot of the examples I am seeing online are using a single view with a hidden div, and the modal window is just populated with the markup from inside the hidden div. In these examples there is only one view (the main view for the page), and the model data behind the hidden div's input fields (etc) are just stored in the model associated with the main view (as opposed to having a separate view and model for the markup I want to display, as would be the case if I were to use a partial view implementation scheme).

I was wondering if anyone can give some insight as to which method is preferred or what the standard practice is (if any) on this. Is this just a matter of personal preference? Is one way more efficient than the other?

Which way should I be doing this:

  • A Partial View for the modal window markup with a separate view/model
  • A Hidden Div within the existing view/model

Note: I am working as part of a development team, so keeping things aligned with standard practice is very important.

Any thoughts on this matter would be greatly appreciated,
Thanks

Edit: I asked my fellow developers who are on the same project how they were doing things similar to this. It looks like no matter how much I debate this issue with them, they all seem to agree that the "best" and "most efficient way" to do this is to render partial views, each with a separate model. I really do not understand why they all think that this method is any more efficient than using hidden divs, but since I am currently "the new guy" I did not push the issue too much and basically just pretended to agree with the point they were trying to make about efficiency.

TL;DR : I am now using partial views, whether I like it or not. I would still like to hear some more opinions on this though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸠魁 2024-12-17 22:40:12

我知道这篇文章已经发布了大约一年,但我想插话一下答案。

你可以随心所欲地翻白眼,但请记住你是初级开发人员,办公室里的其他人可能真的知道他们在说什么。

问问自己,为什么我要用一堆最终用户可能永远不会使用的数据填充隐藏的 div?除非他们每次访问此页面时都打开对话框(如果是,则该页面存在其他根本性错误),否则不将所有数据渲染到 DOM 中会“更有效”。最有效的做法是加载带有部分视图的对话框,并仅在用户请求查看数据时才提取数据。

I know this post is about a year old now, but I wanted to chime in with an answer.

You can roll your eyes all you want but remember you're the junior developer and the other guys in the office might actually know what they're talking about.

Ask yourself, why would I populate a hidden div with a bunch of data that the end user might never even use? Unless they are opening the dialog every time they go to this page (if they are, there is something else fundamentally wrong with the page), it is "more efficient" to not render all of that data into the DOM. The most efficient thing to do is to load the dialog with a partial view and pull the data only when the user requests to see it.

笑,眼淚并存 2024-12-17 22:40:12

如果您使用模态对话框的分部视图,即使模态关闭,分部视图也将保留在页面 HMTL DOM 内。我建议对模式弹出窗口使用单独的视图,并使用 ajax 方法调用它。

部分视图方法:

<div id="popup_window" title="Pop up title" >

<% Html.RenderPartial("_partialviewname", Model); %>

</div>

使用ajax的下一个方法

Html:

<div id="popup_window" title="Pop up title" > <!-- div to render the popup -->
</div>



<a onclick="javascript:ShowPopUp();" >Click here</a> <!--pop up is given by this link-->

Ajax功能部分

function ShowPopup(

$("#popup_window").dialog({
                autoOpen: false,
                modal: true,
                resizable: false,
               // height: 'auto',

                //width: 565
            });

     $.ajax({

                    type: 'GET',
                    url: '/controller/action', //action to return view 
                    cache: false,
                    success: function (data) {
                        $("#popup_window").empty();
                        $("#popup_window").append(data);





                    }
                });

                $(""#popup_window").dialog("open");
}

If you use the partial view for Modal Dialog, the partial view will remain inside page HMTL DOM even if the modal is closed. I suggest to use separate views for modal pop up and call it using ajax methods.

Partial view method:

<div id="popup_window" title="Pop up title" >

<% Html.RenderPartial("_partialviewname", Model); %>

</div>

Next method using ajax

Html:

<div id="popup_window" title="Pop up title" > <!-- div to render the popup -->
</div>



<a onclick="javascript:ShowPopUp();" >Click here</a> <!--pop up is given by this link-->

Ajax function part

function ShowPopup(

$("#popup_window").dialog({
                autoOpen: false,
                modal: true,
                resizable: false,
               // height: 'auto',

                //width: 565
            });

     $.ajax({

                    type: 'GET',
                    url: '/controller/action', //action to return view 
                    cache: false,
                    success: function (data) {
                        $("#popup_window").empty();
                        $("#popup_window").append(data);





                    }
                });

                $(""#popup_window").dialog("open");
}
圈圈圆圆圈圈 2024-12-17 22:40:12

编辑:嗯,很抱歉我的误解。如果您可以重用,如果您最终会切换到基于 AJAX 的刷新或只是想要一个干净/简单的主视图,那么请使用部分视图。

MVC 3 中还有另一种方法,那就是使用 EditorTemplates/DisplayTemplates。它们确实很强大,但需要深入研究才能充分利用。
两种方法都有优点和缺点,这实际上取决于你具体做什么。

长期以来,我发现了两种在 jqUI 对话框中显示部分视图的方法。

前者是通过 AJAX 或 @Html.RenderAction 加载视图
后者,当场景相当静态时,是直接在用于构建对话框的 div 内使用 @Html.RenderPartial

这是我使用的 Ajax 方法:

function MyDlg(options,bottoni) {
    var name = options.name;
    $(name).load(options.url, function () {
        var $jQval = $.validator;
        $jQval.unobtrusive.parse($(this));
        $(name).dialog({
            autoOpen: true,
            width: options.width,
            heigth: options.height,
            resizable: false,
            draggable: true,
            title: options.title,
            modal: true,
            buttons: bottoni
        });
        $(name).dialog("open");
    });
}

如您所见,它接收类似以下内容:

var bottoni = {
    "Cancel": function () {
        // cancel pressed
    },
    "Ok": function () {
        // Ok pressed
    }
}
MyDlg({ name: "Edit", width: "auto", height: "auto", title: 'Edit Stuff', url: mvcUrl }, bottoni);

重要部分是:

Unobtrusive 激活:

var $jQval = $.validator;
$jQval.unobtrusive.parse($(this));

以及对话框在 load 事件中打开的事实。

在控制器中,您只需返回一个 PartialView("_viewName", model)

使用对话框时要小心,清理它们。我更喜欢通过 document.createElement() 以编程方式创建 div,然后在关闭时将其从 DOM 中删除,但在某些情况下我必须将 div 留在 dom 中。

EDIT: Well, sorry for my misunderstand. If you can reuse, if you'll end up switch to AJAX based refresh or simply want to have a clean/simple main view, then go with the partials.

There's another way in MVC 3, that's to use EditorTemplates/DisplayTemplates. They're really powerfull, but require a bit of digging in to be used fully.
Both approach have pros and cons, it really depends on what you're specifically doing.

So long I've found two ways of displaying partial views in jqUI Dialogs.

The former is loading the view via AJAX or @Html.RenderAction
The Latter, when scenario is pretty static, is to use the @Html.RenderPartial directly inside the div used for building the dialog.

Here's the Ajax method I use:

function MyDlg(options,bottoni) {
    var name = options.name;
    $(name).load(options.url, function () {
        var $jQval = $.validator;
        $jQval.unobtrusive.parse($(this));
        $(name).dialog({
            autoOpen: true,
            width: options.width,
            heigth: options.height,
            resizable: false,
            draggable: true,
            title: options.title,
            modal: true,
            buttons: bottoni
        });
        $(name).dialog("open");
    });
}

As you can see, it receives something like:

var bottoni = {
    "Cancel": function () {
        // cancel pressed
    },
    "Ok": function () {
        // Ok pressed
    }
}
MyDlg({ name: "Edit", width: "auto", height: "auto", title: 'Edit Stuff', url: mvcUrl }, bottoni);

The important parts are:

the Unobtrusive activation:

var $jQval = $.validator;
$jQval.unobtrusive.parse($(this));

And the fact that the dialog is opened in the load event.

In the controller you simply return a PartialView("_viewName", model).

Beware when using dialog, to clean 'em up. I prefer to programmatically create the div via document.createElement() and then remove it from DOM upon Closure, but there are scenarios in which I have to leave the div in the dom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文