Search code examples
c#asp.netrazor

How to use DisplayFor to display IPAddress type?


I just started to learn asp.net razor page following the Microsoft Tutorial. and I am trying to make my little web application to show devices, and every devices will contain an IPv4 address. I am using the System.Net to contain IPAddress in my Device model, and use Scaffolder to generate the basic pages. However, when I start my app, it is showing error for the IP address part. Can anyone suggest a way to solve this? Following is my code for my Device model:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Net;

namespace AirFiber.Models
{
    public enum DeviceType
    {
        Master,Node,Switch,Router
    }
    public class Device
    {
        public int DeviceID { get; set; }
        public string DeviceName { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Brand { get; set; }
        public string DeviceModel { get; set; }

        public DeviceType DeviceType { get; set; }

        public AirLink AirLink { get; set; }

        public IPAddress DeviceIP { get; set; }

        public Location Location { get; set; }
    }
}

Following is the code in cshtml page:

@model AirFiber.Pages.Devices.DetailsModel

@{
    ViewData["Title"] = "Details";
}

<h1>Details</h1>

<div>
    <h4>Device</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.DeviceName)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.DeviceName)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.UserName)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.UserName)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.Password)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.Password)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.Brand)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.Brand)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.DeviceModel)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.DeviceModel)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.DeviceType)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.DeviceType)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Device.DeviceIP)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Device.DeviceIP)
        </dd>
    </dl>
</div>
<div>
    <a asp-page="./Edit" asp-route-id="@Model.Device?.DeviceID">Edit</a> |
    <a asp-page="./Index">Back to List</a>
</div>

I know the question is stupid, but can anyone suggest a way to display it in the cshtml? When I google my stupid question, all answer I got is how to get user IP address


Solution

  • I think what's going on here is that @Html.DisplayFor doesn't have a display template for IPAddress so falls back to a default template that ends up not being suitable for IP addresses.

    The work-around is easy: Provide an explicit display template for IPAddress.

    One way to do this is:

    • Create a file named IPAddress.cshtml under Views/Shared/DisplayTemplates.
    • In this file, define how you'd like IP addresses to be rendered. This could be as simple as:
    @model System.Net.IPAddress
    @Model
    

    (Note: While the above link is for ASP.Net Core, I believe the process of creating a display template in legacy ASP.Net is nearly, if not exactly, identical.)