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

  • Home
  • SEARCH
  • 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 1080867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:05:39+00:00 2026-05-16T22:05:39+00:00

I am writing an MVC application, and I wanted to do some extra formatting

  • 0

I am writing an MVC application, and I wanted to do some extra formatting so the phone numbers all are stored the same. To accomplish this I made a simple external function to strip all non-numeric characters and return the formatted string:

public static string FormatPhone(string phone)
{
    string[] temp = { "", "", "" };

    phone = Regex.Replace(phone, "[^0-9]","");

    temp[0] = phone.Substring(0, 3);
    temp[1] = phone.Substring(3, 3);
    temp[2] = phone.Substring(6);

    return string.Format("({0}) {1}-{2}", temp[0], temp[1], temp[2]);
}

There is also regex in place in the model to make sure the entered phone number is a valid one:

[Required(ErrorMessage = "Phone Number is required.")]
[DisplayName("Phone Number:")]
[StringLength(16)]
[RegularExpression("^\\(?([0-9]{3})\\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
 ErrorMessage = "Please enter a valid phone number.")]
public object phone { get; set; }

This is what I did in the controller:

[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
    var customer = customerDB.Customers.Single(c => c.id == id);

    try
    {
        customer.phone = HelperFunctions.FormatPhone(customer.phone);    
        UpdateModel(customer,"customer");                 
        customerDB.SaveChanges();
        return RedirectToAction("Index");
    }
    catch
    {
        var viewModel = new CustomerManagerViewModel
        {
            customer = customerDB.Customers.Single(c => c.id == id)
        };
        return View(viewModel);
    }
}

When I step through this, the string updates then resets back to the format it was before being ran through the function. Also, any of the other fields update with no problem.

Any ideas? Thanks in advance.

  • 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-05-16T22:05:39+00:00Added an answer on May 16, 2026 at 10:05 pm

    Your UpdateModel call is overwriting the customer field. Try swapping these two lines of code:

    try 
    { 
        UpdateModel(customer,"customer");                          <--              
        customer.phone = HelperFunctions.FormatPhone(customer.phone);  <--   
        customerDB.SaveChanges(); 
        return RedirectToAction("Index"); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an ASP.Net MVC application using Jquery to post some data to my
I am writing some integration tests for an ASP.NET MVC application, and I am
I am writing an MVC application with C#. In this one particular section, I
I'm writing Sping MVC based application all my methods inside a controller return ModelAndView
We are writing an ASP.NET MVC application. By default, if the client browser has
I am writing an ASP.Net MVC 3 Web Application using Entity Framework 4.1. My
I'm writing a small Windows application in Visual C++ without MVC. Its really small,
I am writing a web application on Asp.Net MVC 3. Assume that we have
So I'm looking into writing some custom MVC extensions and the first one I'm
I'm writing a mini-MVC application. App architecture: actions/ templates/ site/ app/ styles/ images/ scripts/

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.