MVC Html.FormatValue tutorial with examples

HTML.FormatValue in .NET MVC: A Guide with Code Example

In .NET MVC, HTML.FormatValue is a useful utility method that helps to format values in views. This method is used to display a value in a specified format, making it easier to control the display of values in the user interface. It is particularly useful when you want to display currency, date and time, or number values in a specific format.

In this blog post, we will take a closer look at HTML.FormatValue and how it can be used in .NET MVC, including a code example.

What is HTML.FormatValue?

HTML.FormatValue is a method that takes two arguments: the value to be formatted and the format string. The value to be formatted can be of any data type, such as decimal, int, double, or DateTime. The format string is a string that defines the format in which the value should be displayed.

Here's a simple example of HTML.FormatValue in action:
@Html.FormatValue(12345.67, "C")
The above code will output the value 12345.67 as a currency, which would be displayed as "$12,345.67". The "C" format string is used to display the value as a currency.

How to use HTML.FormatValue in .NET MVC

To use HTML.FormatValue in .NET MVC, you first need to add a reference to the System.Web.Mvc namespace in your view. This can be done by adding the following line of code at the top of your view:
@using System.Web.Mvc
Next, you can use HTML.FormatValue in your view to format values as needed. Here's a code example that demonstrates how to use HTML.FormatValue to display a date value in a specified format:
@{
    DateTime date = DateTime.Now;
}

Date: @Html.FormatValue(date, "d")

The above code will output the current date in the format "MM/dd/yyyy". HTML.FormatValue Format Strings

The format string is a crucial part of HTML.FormatValue. It defines the format in which the value should be displayed. There are several predefined format strings that you can use in your code, including:
  • "C" for currency
  • "d" for short date
  • "D" for long date
  • "F" for full date and time
  • "g" for general date/time
  • "G" for general date/time with second fraction
  • "n" for number
  • "p" for percentage
  • "R" for round-trip date/time
  • "t" for short time
  • "T" for long time
You can also create custom format strings by combining the predefined format strings or by using custom format patterns.

Conclusion

In this blog post, we took a closer look at HTML.FormatValue in .NET MVC and how it can be used to format values in views. Whether you want to display currency, date and time, or number values in a specific format, HTML.FormatValue makes it easy to control the display of values in the user interface. By using the predefined format strings or custom format strings, you can create the exact display format that you need for your values.

For complete information about Html.FormatValue method you can check this: HtmlHelper.FormatValue Method
 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
1 + 2 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice