IP の整数表現によってドット付き 10 進オクテットで表示された IP アドレスを持つ webgrid テーブルを並べ替えようとしています。asp.net Webフォームでは、ソート式をデータベースの他の列に設定して、適切なソートを実現できました。
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);
})
)
)