webgrid排序基于数字表示的ipaddress

时间:2013-07-03 13:22:21

标签: asp.net-mvc entity-framework sorting

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

有没有办法“正确”对我的网络网格中的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);

        })
    )
)

1 个答案:

答案 0 :(得分:1)

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

我以前执行此操作的代码如下: https://gist.github.com/TechplexEngineer/5951594