0

I have a POCO class for an edit model

class PersonEditModel {
  public string ScreenName{get;set;}
}

It's common for ScreenName to be null when my domain model is converted into this PersonEditModel.

When I present this in MVC 2 form using the html helpers I get back an empty string (if the user didn't add anything into screenname), because the model binder can't tell the difference between empty and null string value.

Is there a way to tell the model binder to automap a particular string value to null?

So, instead of getting back ScreenName as "", I would get back null. When I persist this back to the Domain model, it's alot cleaner for the data model.

2

1 Answer 1

0

Have you tried adding this attribute? It should be true by defualt but something might be different with your configuration.

[ConvertEmptyStringToNull=true]
public string ScreenName{get;set;} 

Another option that is less clean is to put a private variable behind ScreenName and if its value is an empty string, then return null on the get.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.