0

Model

public Nullable<decimal> extensionattribute9 { get; set; }

Controller

public class UsersController : Controller
{
    private PortalTestEntities db = new PortalTestEntities();

    // GET: Users
    public ActionResult Index()
    {
        return View(db.Users.ToList());
    }      
}

View

        <th>
        @Html.DisplayNameFor(model => model.extensionattribute9)
    </th>

What it looks like

How can i display that number as MB so /1024?

  • 1
    See also: http://stackoverflow.com/questions/14488796/does-net-provide-an-easy-way-convert-bytes-to-kb-mb-gb-etc -- Btw. `bytes / 1024` is `KB`, not `MB`. – Corak Jun 10 '16 at 14:27

2 Answers2

1

The "pure" way would be to add that to the model:

public Nullable<decimal> extensionattribute9InMB 
{ 
    get {return extensionattribute9 / 1024m;} 
}

And then choose which property you wanted to display in the view. The hacky way would be to do that calculation directly in the view.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

First of all, DisplayNameFor helper method returns a display name of your model property, so you do not want to use that to display the value.

For converting bytes to megabytes, you might consider writing an extension method and use it.

public static class DecimalExtensions
{
    public static decimal? ToMegaBytes(this decimal? value)
    {
        if (value != null)
        {
            return value / 1024;
        }
        return null;
    }
}

and in your view

<span>@Model.extensionattribute9.ToMegaBytes()</span> 
Shyju
  • 214,206
  • 104
  • 411
  • 497