0

I'm working on an ASP.NET MVC application where I have a set of view models used for different forms. The client is mainly copying data from an Excel sheet into these forms.

I.e. a view model for person:

 public class Person
 {
     [Required]
     [Display(Name = "First name")]
     public string FirstName { get; set;}
 }

When client is pasting a value from Excel into this field, the actual value stored in the database (using Entity Framework 6 fyi) contains an invisible line break.

Is there any way to make a custom data annotation which, essentially, parses the input string and returns the parsed value? Or do I have to make a method for parsing the input and use it everywhere in my code where I save data?

Thanks in advance.

Bo Mortensen
  • 935
  • 1
  • 13
  • 31
  • I think this [question](http://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custo) might solve your problem – sjokkogutten Nov 24 '15 at 12:45
  • 1
    how about a custom model binder for that specific action ? which parses the correct data to the property instead of doing it in the data annotation ? – Idrees Khan Nov 24 '15 at 12:46
  • A data annotation wont do anything to solve this. You need a custom model binder. –  Nov 24 '15 at 12:51
  • thanks all :-) guess I mixed annotation and model binder up. I created a model binder from the code in the link you wrote @sjokkogutten and it worked right away! – Bo Mortensen Nov 24 '15 at 13:00

0 Answers0