model-binders

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

安稳与你 提交于 2019-12-18 10:43:30
问题 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)? 回答1: 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

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

心不动则不痛 提交于 2019-11-29 23:06:50
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)? 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(NameValueCollection

MVC DateTime binding with incorrect date format

故事扮演 提交于 2019-11-26 03:01:12
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 to

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

元气小坏坏 提交于 2019-11-26 01:41:14
问题 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

MVC DateTime binding with incorrect date format

吃可爱长大的小学妹 提交于 2019-11-26 01:05:50
问题 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