Microsoft MVC bloggers

Sponsors

February 2009 - Posts

Splitting DateTime - Unit Testing ASP.NET MVC Custom Model Binders
I've got this form for users to create an event. One of the fields is a DateTime, like this: And that's kind of lame as it's hard to type in a Date and a Time at the same time. It's also no fun. Most sites would have those separated, and ideally use some kind of Calendar Picker with jQuery or something. But when you post a form like this back to a Controller Action, it doesn't exactly line up neatly with a System.DateTime object. There's no clean way to get partials like this and combine them into a single DateTime. The "ViewModel" in doesn't match the Model itself. I could certainly make my method take two DateTimes, along with the other fields, then put them together later. It could get mess though, if...
ASP.NET Wire Format for Model Binding to Arrays, Lists, Collections, Dictionaries
Levi Broderick, a Senior Developer on ASP.NET MVC and all around smart dude, posted some details to an internal Microsoft mailing list today and I thought it was worth sharing. Levi agreed and I've expanded on it some. Phil blogged some about Model Binding to a List last October . The default model binder will handle a number of collection types for you, if you play by the rules and make your HTML form elements follow a certain format. If the method signature looks like this: public ActionResult Blah(Person[] people) { // ... } And we are given this input in our HTML: <input type="text" name="people[0].FirstName" value="George" /> <input type="text" name="people[0].LastName" value...
IPrincipal (User) ModelBinder in ASP.NET MVC for easier testing
ModelBinders are great. I've blogged a bit about them before like the File Upload Model Binder . I am working on some code like this: [Authorize] public ActionResult Edit(int id) { Dinner dinner = dinnerRepository.FindDinner(id); if (dinner.HostedBy != User.Identity.Name) return View("InvalidOwner"); var viewModel = new DinnerFormViewModel { Dinner = dinner, Countries = new SelectList(PhoneValidator.Countries, dinner.Country) }; return View(viewModel); } It's pretty straight forward, but this Controller knows too much. It's reaching into implicit parameters. The id was passed in, but the User is actually a property of the Controller base class and ultimately requires an HttpContext. Having this method "know" about...
More Posts