简体   繁体   English

Asp.Net MVC Razor View服务器端的动态div显示顺序

[英]Dynamic div display order on Asp.Net MVC Razor View Server Side

Is it possible to change display order of divs when rendering View Page on Server Side? 在服务器端渲染视图页面时是否可以更改div的显示顺序? Every single div has different c# and html content, so it's not possible to generate them by loop. 每个div都有不同的c#和html内容,因此无法通过循环生成它们。

One option is display as is and reordering in client side when page OnReady by jQuery. 一种选择是按原样显示并在jQuery页面OnReady时在客户端重新排序。 But it has some delay time to rearrange divs. 但是重新分配div会有一些延迟时间。

Another option is generate view page for every div, then loop div ids, render content with @Html.Action or @Html.RenderPartial . 另一个选择是为每个div生成视图页面,然后循环div id,使用@Html.Action@Html.RenderPartial呈现内容。 It is not feasible when items are >20 and pages are >100 当项目> 20并且页面> 100时不可行

Any solution on server side would be appreciated. 服务器端的任何解决方案将不胜感激。

Foo.cshtml page: Foo.cshtml页面:

<div class="container">
    <div id="a">
       //some c# codes to render html
    </div>
    <div id="b"> 
     //some other c# codes to render html
    </div>
    <div id="c"> 
     //some another c# codes to render html
    </div>
</div>

When Cookie value for ViewOrder: c,b,a 当ViewOrder的Cookie值:c,b,a

Expected output: 预期产量:

<div class="container">
    <div id="c"> 
     //some another c# codes to render html
    </div>
    <div id="b"> 
     //some other c# code to render html
    </div>
    <div id="a">
       //some c# codes to render html
    </div>
</div>

Possible solution on client side with jQuery jQuery在客户端的可能解决方案

$.fn.orderChildren = function(order) {
    this.each(function() {
        var el = $(this);
        for(var i = order.length; i >= 0; i--) {
            el.prepend(el.children(order[i]));
        }
    });
    return this;
};


$(".user").orderChildren(["c","b","a"]);

For server-side reordering in cshtml, split the ViewOrder into an array of strings and iterate through it: 为了在cshtml中进行服务器端重新排序,请将ViewOrder拆分为字符串数组并对其进行迭代:

foreach(var div in ViewOrder.Split(",".ToCharArray()) {
    switch (div) {
        case "a":
            <div id="a">
            //some c# codes to render html
            </div>
            break;
        case "b":
            <div id="b">
            //some c# codes to render html
            </div>
            break;
        ... etc ...
}}

Note that there is also a CSS solution using FlexBox and the CSS order: attribute, as illustrated in this answer . 请注意,还有一个使用FlexBox和CSS order:属性的CSS解决方案,如本答案所示

If you don't want to create partials (which I would still consider feasible, since you have the markup anyway in your page), you can loop over your search predicates and use switch to render your parts: 如果您不想创建局部(我仍然认为可行,因为您的页面中仍然有标记),则可以遍历搜索谓词并使用switch来呈现零件:

Your controller would add the order to your model: 您的控制器会将订单添加到模型中:

//Model
class MyModel {
    //...
    public string[] DivOrder { get; set; };
    //...
}

//Controller Action
MyModel model = new MyModel();
//...
model.DivOrder = new { "c", "b", "a" };
//...

Your view would rearrange the output based on the order: 您的视图将根据顺序重新排列输出:

<div class="container">
    @foreach(string key in Model.DivOrder) {
        switch (key) {
            case "a":
                <div id="a">
                    //some c# codes to render html
                </div>
                break;
            case "b":
                <div id="b">
                    //some other c# codes to render html
                </div>
                break;
            case "c":
                <div id="c">
                    //some another c# codes to render html
                </div>
                break;
        }
    }
</div>

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

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