Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7751159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:28:34+00:00 2026-06-01T11:28:34+00:00

I am working on an asp.net MVC 3 application. I have a C# function

  • 0

I am working on an asp.net MVC 3 application.

I have a C# function that takes a string as an input, formats it and then returns the formatted string.

I have a .cshtml file where I fill data in a webgrid.

When filling the data in the webgrid, I want to format it first through a C# function and then fill the results.

In asp.net I can simply use the code behind to do it. but I am new to MVC 3 so any suggestions on how to do it is greatly appreciated.

I heard it should be done in the action. can you help me on how to do that?

the C# function look like this

public string FormatString (string input)
{
    // I Format the string and return it
} 

the webgrid look like this

<div id="GridDiv">
@grid.GetHtml(
        htmlAttributes: new { id = "grid" },
        tableStyle: "grid",
        headerStyle: "header",
        rowStyle: "row",
        footerStyle: "footer",
        alternatingRowStyle: "altRow",
        columns: grid.Columns(
            grid.Column("Name", "Name", style: "Centered", canSort: true),
            grid.Column("Address", "Address", canSort: true),
            grid.Column("City", "City", canSort: true),
            grid.Column("PhoneNumber", "Phone Number", style: "Centered"),
        ))

I want to format the Name, Address, City, and PhoneNumber

Thanks a lot for any help

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:28:35+00:00Added an answer on June 1, 2026 at 11:28 am

    OK, personally I have never used the Grid helper, but I assume this would be the general direction.
    Let’s say the Name/Phone/City/Address are properties of a Person class.

    Build an appropriate view model

    public sealed class MyViewModel
    {
        public string Name {get; set;}
        public string Address {get; set;}
        public string City {get; set;}
        public string PhoneNumber {get; set;}
    
        public MyViewModel(Person person)
        {
            FillViewModel(person); // either copy the given properties by hand, use Automapper, or write your own simple (although quite general) mapping method using reflection
            Address = FormatString(Address);
            City = FormatString(City);
            PhoneNumber = FormatString(PhoneNumber);
        }
        public MyViewModel()
        { 
        }
    }
    

    Use it in controller action

    public ActionResult ShowGrid(int personId)
    {
        var person = SomeKindOfDatabaseAccess.GetPerson(personId);
        var viewModel = new MyViewModel(person);
        return View(viewModel);
    }
    

    And then use this model on your page, so when you pass PhoneNumber, City and Address to the grid in the view, you’ll have them pre-formatted.

    EDIT: Regarding ‘where to put the FormatString method’

    As I see it, you can put it in the ‘Person’ model class if you’re going to use it in the Person-related business only:

    (If the Person class is generated from e.g. EF, you can do it in a different file so it looks neat, like below)

    public sealed partial class Person
    {
         public string FormatString(string input)
         {
             ...
         }
    }         
    

    or

    public sealed partial class Person
    {
         public static string FormatString(string input)
         {
             ...
         }
    }         
    

    or if you’re feeling fancy (or – more practically – you’re going to use it in any other situation for string formatting), you can define the method as a string extension for convenience:

    public static class Extensions
    {
         public static string FormatString(this string input)
         {
             ...
         }
    }
    

    And then: PhoneNumber = PhoneNumber.FormatString();

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an asp.net MVC application. I have a class that wraps a
I have an ASP.NET MVC application that I'm working on. I've been developing it
I am working with an ASP.NET MVC application. I have one master page having
I am working on an ASP.NET MVC application that contains a header and menu
I am working on asp.net MVC 3 application. I have created a Razor view
I am working in an ASP.NET MVC Application. I have a view model as
I am working on a ASP.NET MVC application where we have to write our
I'm working on my first ASP.NET MVC application and have a strange issue. All
I am working on maintaining a ASP.NET MVC application that has the following coding
I am working on asp.net MVC application. I have a folder with some sub

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.