I am having problems with (2) fields when saving objects in an ASP.NET MVC4 application:
As you know, these fields will have special characters @ and - as a rule. Here is my controller code using ValueInjecter to input model classes from a hydrated object through 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 will be truncated. Terminated"
Well, at first glance you can say that this is the size of the field in SQL, but the true problem is masked due to these special characters. If I enter the following for the fields:
tel .: 11234565454514564561
email: blahblahblahblah
... then everything works .
However, if I enter:
tel .: 123-456-7890
Email: test@wow.com
I get the error "String or binary data will be truncated." So I found a job, but it seems completely unnecessary and probably wrong. If I explicitly map the values ββfrom the associated model object to the entity using C # escape characters, then my values ββwill be inserted as shown below:
public ActionResult Create(TestViewModel testViewModel) { using (var context = new MyEntities()) { var person = new Person(); person.InjectFrom(testViewModel);
Well, honestly, I donβt know where the criminal is. Is this a MVC model binding problem, an Entity Framework problem, a C # deal, or a ValueInjecter? I tried using annotations for my ViewModel properties to dictate data types such as Phone and Email , but this did not stop the problem. This was done only by being explicit in the code, eluding those characters to do so.
What am I doing wrong because I know that fields like email and phone with special characters are stored all the time in EF using MVC? What can I do to avoid having to sprinkle these lines of code every time I have a value with a special character?
Thanks!!