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 8349831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:07:37+00:00 2026-06-09T08:07:37+00:00

I am having problems with (2) fields when saving entities in my ASP.NET MVC4

  • 0

I am having problems with (2) fields when saving entities in my ASP.NET MVC4 application:

  • email
  • phone

As you know, these fields will have special characters @ and - typically. Here is my code from the controller using ValueInjecter to inject my model classes from the hydrated object via Model Binding:

public ActionResult Create(TestViewModel testViewModel)
{
  using (var context = new MyEntities())
  {
    var person = new Person();
    person.InjectFrom(testViewModel);
    context.Person.AddObject(person);
    context.SaveChanges();
  }
}

The error I am getting is the following:

“String or binary data would be truncated. The statement has been
terminated”

Ok on 1st glance you might say it’s the size of the field in SQL, but the true issue is masqueraded because of those special charachters. If I enter the following for the fields:

phone: 11234565454514564561
email: blahblahblahblah

…then everything works.

However if I enter:

phone: 123-456-7890
email: test@wow.com

I get the error about the “String or binary data would be truncated”. So I found a work-around but this seems completly unnecessary and probably the wrong way. If I explicitly map the values from the model bound object to the entity, using the C# escape characters, then my values will insert as shown below:

public ActionResult Create(TestViewModel testViewModel)
{
  using (var context = new MyEntities())
  {
    var person = new Person();
    person.InjectFrom(testViewModel);

    //Explicitly map items preserving special charachters
    person.EmailAddress = @testViewModel.EmailAddress;
    person.Phone = @testViewModel.Phone;

    context.Person.AddObject(person);
    context.SaveChanges();
  }
}

OK, so honestly I don’t know where the culprit lies. Is this a MVC model binding issue, an Entity Framework issue, a C# deal, or ValueInjecter? I tried using annotations on my ViewModel properties to dictate the data types like Phone and Email but that didn’t stop the issue. Only by being explicit in code by escaping those characters was a way to do it.

What am I doing incorrectly because I know fields like email and phone with special characters are saved off all the time to EF with MVC? What can I do to prevent having to sprinkle in those lines of code every time I have a value with a special character?

Thanks!!

  • 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-09T08:07:40+00:00Added an answer on June 9, 2026 at 8:07 am

    Well in typical fasion I figure it out 10 seconds after posting the question that I have spent 3 hours trying to figure out:

    As @CodeMaster noted, those lines of code were incorrect anyway. I started playing with some of the other fields and I figured out the culprit.

    The code smaple I gave was condensed for brevity, but I was injecting into (2) different entites: Person and Contact. It turns out both tables have a ‘Title’ field. I was only focused on the ‘Title’ field in the Person table which had a length of ’50’ like below:

    <Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="50" />

    It turns out the Contact field had the exact smae field I was unaware of but only a length of ‘8’ as below:

    <Property Name="Title" Type="nvarchar" MaxLength="8" />

    This caused the error and it was indeed legitimate. ValueInjecter mapped the same single ‘Title’ value to both entities when I only wanted the one and caused the issue. Have to be careful because ValueInjectoer does not discriminate if names match (as it should not) but this cause my problem.

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

Sidebar

Related Questions

I am having problems with this and I'm hoping it's possible. I have a
I am having problems styling form fields with CSS. As you can see below
I'm having problems with an SQL query used to display custom profile fields and
I'm having problems inserting commas (,) in my text fields in html. When I
I have having a problem saving the sign up form using Devise. I am
I'm having a problem with ASP.net redirection. The Copy() method below is called from
I'm having some problems limiting fields in result set from mongodb using the Ruby
I'm having problems updating records to contain NULL values - in particular, a field
I am a newbee to solr.I am having a problem with adding fields/metadata to
I'm having some problem with validations for multiple fields, specifically with case-sensitive unique validations

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.