Это проблема, которую вы можете получить с любой текстовой областью, если люди добавляют лишние пробелы или символы новой строки.
Я заменил DefaultModelBinder на тот, который обрезает любой тип строки (это измененная версия, которую я нашелв сети, к сожалению, я не сделал заметки на сайте, поэтому не могу его отнести)
public class TrimmingModelBinder : DefaultModelBinder
{
protected override void SetProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
System.ComponentModel.PropertyDescriptor propertyDescriptor,
object value) {
string modelStateName = string.IsNullOrEmpty(bindingContext.ModelName) ?
propertyDescriptor.Name :
bindingContext.ModelName + "." + propertyDescriptor.Name;
// only process strings
if (propertyDescriptor.PropertyType == typeof(string))
{
if (bindingContext.ModelState[modelStateName] != null)
{
// modelstate already exists so overwrite it with our trimmed value
var stringValue = (string)value;
if (!string.IsNullOrEmpty(stringValue))
stringValue = stringValue.Trim();
value = stringValue;
bindingContext.ModelState[modelStateName].Value =
new ValueProviderResult(stringValue,
stringValue,
bindingContext.ModelState[modelStateName].Value.Culture);
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}
else
{
// trim and pass to default model binder
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, (value == null) ? null : (value as string).Trim());
}
}
else
{
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}
}
}
Затем в application_start просто подключите его так:
ModelBinders.Binders.DefaultBinder = new Kingsweb.Extensions.ModelBinders.TrimmingModelBinder();
И всеваши переменные будут обрезаны к тому времени, когда они попадут в методы действия.