Reputation: 135
Trying to force text in certain fields in a view that is adding records to the DB via Entity framework to uppercase.
Can I do it in the view EditorFor or can I do it in the controller to all fields easily before firing db.SaveChanges()?
Upvotes: 0
Views: 1873
Reputation: 11971
You could loop through the properties on your ViewModel
server side using a helper method.
public T ViewModelToUpper<T>(T viewModel) where T : class
{
foreach (var property in viewModel.GetType().GetProperties())
{
var value = property.GetValue(viewModel, null);
if (value is string)
{
property.SetValue(viewModel, value.ToString().ToUpper());
}
}
return viewModel;
}
Then you can call viewModel = ClassName.ViewModelToUpper(viewModel)
. Now you don't have to worry about doing it for every string
property as this will happen anyway.
Upvotes: 1
Reputation: 1744
You can do this in the UI with jQuery if you'd like. It's a little weird from a user perspective to be typing and have everything converted to uppercases (I'd check my caps lock). Convert to uppercase as user types using javascript
Your other option, better IMO, is to do this in the controller. You can use ToUpper()
on the strings. Keep Globalization in mind.
Upvotes: 0