天道酬勤,学无止境

model-binders

How to test custom Model Binders in ASP.NET MVC?

Question I've written some custom model binders (implementing IModelBinder) in our ASP.NET MVC application. I'm wondering what is a good approach to unittest them (binders)? Answer1 I did it this way: var formElements = new NameValueCollection() { {"FirstName","Bubba"}, {"MiddleName", ""}, {"LastName", "Gump"} }; var fakeController = GetControllerContext(formElements); var valueProvider = new Mock<IValueProvider>(); var bindingContext = new ModelBindingContext(fakeController, valueProvider.Object, typeof(Guid), null, null, null, null); private static ControllerContext GetControllerContext

2021-05-31 15:44:02    分类:技术分享    asp.net   asp.net-mvc   unit-testing   model-binders

MVC DateTime binding with incorrect date format

Question Asp.net-MVC now allows for implicit binding of DateTime objects. I have an action along the lines of public ActionResult DoSomething(DateTime startDate) { ... } This successfully converts a string from an ajax call into a DateTime. However, we use the date format dd/MM/yyyy; MVC is converting to MM/dd/yyyy. For example, submitting a call to the action with a string '09/02/2009' results in a DateTime of '02/09/2009 00:00:00', or September 2nd in our local settings. I don't want to roll my own model binder for the sake of a date format. But it seems needless to have to change the action

2021-03-31 10:37:11    分类:技术分享    asp.net-mvc   datetime   localization   formatting   model-binders

Best way to trim strings after data entry. Should I create a custom model binder?

Question I'm using ASP.NET MVC and I'd like all user entered string fields to be trimmed before they're inserted into the database. And since I have many data entry forms, I'm looking for an elegant way to trim all strings instead of explicitly trimming every user supplied string value. I'm interested to know how and when people are trimming strings. I thought about perhaps creating a custom model binder and trimming any string values there...that way, all my trimming logic is contained in one place. Is this a good approach? Are there any code samples that do this? Answer1 public class

2021-03-27 05:53:12    分类:技术分享    asp.net   asp.net-mvc   model-binders