First thing to do is to make a new shiny custom model binder. I have done mine by inheriting from DefaultModelBinder (which you will need to include System.Web.Mvc to access).
using System; using System.ComponentModel; using System.Web.Mvc; namespace Infrastructure.OFP.ModelBinders { public class MyUserModelBinder : DefaultModelBinder { protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) { if (propertyDescriptor.PropertyType == typeof(DateTime?) && propertyDescriptor.Name == "DateOfBirth") { var request = controllerContext.HttpContext.Request; var prefix = propertyDescriptor.Name; //remember I am a Brit so this will give me dd/mm/yyyy format - this may not work for you :) //also since this is a binder specific for one particular form the hardcoding of // MyUser.DateOfBirth is suitable //AND... I am using Value because I am using a Nullablevar date = string.Format("{0}/{1}/{2}", request["MyUser.DateOfBirth.Value.Day"], request["MyUser.DateOfBirth.Value.Month"], request["MyUser.DateOfBirth.Value.Year"]); DateTime dateOfBirth; if (DateTime.TryParse(date, out dateOfBirth)) { SetProperty(controllerContext, bindingContext, propertyDescriptor, dateOfBirth); return; } else { bindingContext.ModelState.AddModelError("DateOfBirth", "Date was not recognised"); return; } } base.BindProperty(controllerContext, bindingContext, propertyDescriptor); } } }
This new Model Binder will need to be registered to be used when binding a MyUser object. You do this in the Global.asax Application_Start() like so:
protected void Application_Start() { ModelBinders.Binders[typeof(MyUser)] = new MyUserModelBinder(); AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); }
For reference the drop downs I created to populate the nullable DateTime property of DateOfBirth is as follows:
@Html.DropDownListFor(m => m.MyUser.DateOfBirth.Value.Day, new SelectList(Model.Days, "Key", "Value")) @Html.DropDownListFor(m => m.MyUser.DateOfBirth.Value.Month, new SelectList(Model.Months, "Key", "Value")) @Html.DropDownListFor(m => m.MyUser.DateOfBirth.Value.Year, new SelectList(Model.Years, "Key", "Value")) @Html.ValidationMessageFor(m => m.MyUser.DateOfBirth)
This works as I expected it to. It validates the property as expected using the DataAnnotations that I have defined for it.
Hello Colin,
ReplyDeleteAny chance you can provide a complete example of your solution?
Thanks,
Andreas
Yep, complete example is what would help. Thank you,
ReplyDeleteAdmittedly the article is a little blunt and straight down to code, but what is it you feel is incomplete about the example exactly? Maybe I can elaborate. Although bear in mind I wrote this almost a year ago :)
DeleteThis was extremely helpful Colin. Great work!
ReplyDeleteYou did really good work. I really appreciate your new and different post. Please guys keep it up and share with us some unique post in the futureMenmyshopTouch Hypersonic Double Din Player MirrorJ3L Touch Double Din PlayerHypersonic Double Din Player MirrorJ3L Double Din Player Mirror
ReplyDeleteCBSE open schoolcbse privatebanzaraMalhotra law house
Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan. Shipping worldwide. Stitching option available.
ReplyDeleteOnline Custom Essay Services are affordable, with rates that are relatively lower in comparison with other Legitimate Custom Essay Writing Services since they allow learners to estimate charges when placing Legitimate Custom Writing Service Orders.
ReplyDeleteBuy online wholesale printed candle boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!
ReplyDeleteBuy online wholesale printed custom lipstick boxes and packaging at easy prices and shipping worldwide, with 24/7 customer support. Get a free custom quote now!!
ReplyDeleteBlackbird packaging provide you with any size, shape design, cbd bath bomb boxes sooner than any other packaging competitor in the market
ReplyDeletePackaging town is a one-stop solution for the short-run tincure packaging needs, and supplies. At affordable prices. Request a free quote now.
ReplyDeleteBlackbird packaging provide you with any size, shape design, custom bath bomb display boxes sooner than any other packaging competitor in the market
ReplyDeleteGreat Article
ReplyDeleteCloud Computing Projects
Networking Projects
Final Year
Projects for CSE
JavaScript Training in Chennai
JavaScript
Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,
Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.
Angular Training
Hello, This is a Nitu roy very interesting blog. this content is written very well This is an amazing
ReplyDeletepost, keep blogging.thanks for sharing.Anyways I am here now and could just like to say thank
for a tremendous post and a all round entertaining website.
Guwahati Escort
Call girl service in Guwahati
Hello, My Dearest Friends Super class independent Escorts Services available incall & outcall
ReplyDeleteservice in VIP hotels.and call girls available for booking 24x7 hours IF You
want Physical Relationship any time contact me & ?meet with me to get my
number from website and call
Thank You & Visit Again""_____
Guwahati Call Girls
Call Girls in Guwahati
Maybe you are also looking for the right person to ensure the higher quality
ReplyDeletesex and that’s why you are in this place because here we are going to share
the data and information of Guwahati Escorts.
Guwahati escorts
Escorts in Guwahati
Blackbird packaging company can provide you with any size, shape design, and packaging for bath bombs sooner than any other packaging competitor in the market
ReplyDeletePackaging town is a one stop solution for the short-run custom soap boxes needs and supplies. At affordable prices. Request a free quote now.
ReplyDeleteExplore the profiles of guwahati escorts, guwahati call girls, call girls in guwahati and
ReplyDeleteescorts in guwahati for female companionship in top rated hotels.
Get affordable escort services in guwahati hotels now.
Guwahati call girl
Guwahati escort service
Blackbird packaging provide you with any size, shape design, cbd bath bombs wholesale sooner than any other packaging competitor in the market
ReplyDeleteBlackbird packaging provide you with any size, shape design, bath bomb boxes sooner than any other packaging competitor in the market
ReplyDeletePackaging town is a one stop solution for the short-run custom pillow boxes needs and supplies. At affordable prices. Request a free quote now.
ReplyDeleteBlackbird packaging provide you with any size, shape design, bath bomb boxes sooner than any other packaging competitor in the market
ReplyDeletePackaging town is a one stop solution for the short-run custom candle boxes needs and supplies. At affordable prices. Request a free quote now.
ReplyDeleteBlackbird packaging provide you with any size, shape design, cookie boxes wholesale sooner than any other packaging competitor in the market
ReplyDeletePackaging town is a one stop solution for the short-run rigid boxes needs and supplies. At affordable prices. Request a free quote now.
ReplyDeletebuy tops for men online from dshred at easy prices delivered at your doorstep. Get free shipping
ReplyDeleteBuy shorts for men from ndure at best prices delivered at your doorstep. Get free shipping
ReplyDeleteBuy basic cotton tees online from ndure at easy prices delivered at your doorstep. Get free shipping
ReplyDeleteBuy lightweight vest online from Dshred at best prices delivered at your doorstep. Get free shipping
ReplyDeleteBuy online electronics from Al-Fatah at best prices delivered at your doorstep. Get free shipping
ReplyDeleteBuy online grocery from Al-Fatah at best prices delivered at your doorstep. Get free shipping
ReplyDeleteBuy men footwear online in Pakistan from Al-Fatah at best prices delivered at your doorstep. Get free shipping.
ReplyDeleteExcellent. Good blog. Keep sharing. I love them Are you also searching for write my nursing research paper? we are the best solution for you. We are best known for delivering nursing research paper writing services to students without having to break the bank
ReplyDeleteyour website is very nice, the article is very enlightening, I wish you continued success. สล็อต joker
ReplyDelete