0

我正在尝试通过 IP 的整数表示对具有以点分十进制八位字节显示的 IP 地址的 webgrid 表进行排序。在 asp.net webforms 中,我可以将排序表达式设置为数据库中的另一列以实现正确的排序。

有什么方法可以“正确”对我的 webgrid 中的 IP 地址列进行排序?

我有一个看起来像这样的模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Net;

namespace pcnweb_mvc3_v1.Models
{
    public partial class UPS
    {
        public UPS()
        {
            //this.Switches = new List<Switch>();
        }

        [Key]
        public int upsRecId { get; set; }
        [Required, StringLength(32)]
        public string Name { get; set; }
        [StringLength(80)]
        public string Description { get; set; }
        [Required, StringLength(32)]
        public string Size { get; set; }
        [StringLength(56)]
        public string Location { get; set; }
        [StringLength(32)]
        public string Level { get; set; }
        [StringLength(16)]
        public string Circuit { get; set; }
        [StringLength(32)]
        public string Power_Feed { get; set; }

        [Required]
        [RegularExpression(@"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)|DHCP", ErrorMessage = "IP Address must be in the form of four octets 0-255: 255.255.255.255 or DHCP")]
        public string IP_Address {
            get 
            {
                if (ipOctet1 == null | ipOctet2 == null | ipOctet3 == null | ipOctet4 == null)
                    return "DHCP";
                return ipOctet1 + "." + ipOctet2 + "." + ipOctet3 + "." + ipOctet4; 
            }
            set 
            {
                if (value == "DHCP")
                {
                    ipOctet1 = null;
                    ipOctet2 = null;
                    ipOctet3 = null;
                    ipOctet4 = null;
                    return;
                }

                //@todo Parseing could throw a number of errors: ArgumentNullException, FormatException, Exception
                byte[] temp = IPAddress.Parse(value).GetAddressBytes();
                ipOctet1 = temp[0];
                ipOctet2 = temp[1];
                ipOctet3 = temp[2];
                ipOctet4 = temp[3];
            }
        }

        public long ipTotal
        {
            get 
            {
                //(first octet * 2^24) + (second octet * 2^16) + (third octet * 2^8) + (fourth octet)
                return (long)Math.Pow(24, 2) * (long)ipOctet1 + (long)Math.Pow(16, 2) * (long)ipOctet2 + (long)Math.Pow(8, 2) * (long)ipOctet3 + (long)ipOctet4;
            }
        }

        private Nullable<int> ipOctet1 { get; set; }
        private Nullable<int> ipOctet2 { get; set; }
        private Nullable<int> ipOctet3 { get; set; }
        private Nullable<int> ipOctet4 { get; set; }

        //public virtual ICollection<Switch> Switches { get; set; }
    }
}

这是 webgrid cshtml 页面:

@model IEnumerable<pcnweb_mvc3_v1.Models.UPS>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

@{
    var grid = new WebGrid(Model, defaultSort: "Name", canPage: true, rowsPerPage:25);
}
@grid.GetHtml(
    columns: grid.Columns(
        //grid.Column("upsRecId"),
        grid.Column("Name"),
        grid.Column("Description"),
        grid.Column("Size"),
        grid.Column("Location"),
        grid.Column("Level"),
        grid.Column("Circuit"),
        grid.Column("Power_Feed"),
        grid.Column("IP_Address"),
        grid.Column(
        format: (item) =>
        {
            var links = Html.ActionLink("Edit", "Edit", new { id = item.upsRecId }) + " | " +
                        Html.ActionLink("Details", "Details", new { id = item.upsRecId }) + " | " +
                        Html.ActionLink("Delete", "Delete", new { id = item.upsRecId });

            return Html.Raw(links);

        })
    )
)
4

1 回答 1

1

所以我有一个解决方法,它不是最好的,但它有效。我将 IP 地址从存储在数据库中的四个八位字节转换为十进制表示,将其传递给 webgrid,以便它可以正确地对值进行排序,然后提供自定义格式化函数来获取十进制值并将其转换回点分十进制形式。

我用来执行此操作的代码在这里: https ://gist.github.com/TechplexEngineer/5951594

于 2013-07-08T21:17:49.267 回答