Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 656267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:41:45+00:00 2026-05-13T22:41:45+00:00

I’m rather new to MVC and as I’m getting into the whole framework more

  • 0

I’m rather new to MVC and as I’m getting into the whole framework more and more I’m finding the modelbinders are becoming tough to maintain.

Let me explain…

I am writing a basic CRUD-over-database app. My domain models are going to be very rich. In an attempt to keep my controllers as thin as possible I’ve set it up so that on Create/Edit commands the parameter for the action is a richly populated instance of my domain model. To do this I’ve implemented a custom model binder.

As a result, though, this custom model binder is very specific to the view and the model. I’ve decided to just override the DefaultModelBinder that ships with MVC 2. In the case where the field being bound to my model is just a textbox (or something as simple), I just delegate to the base method. However, when I’m working with a dropdown or something more complex (the UI dictates that date and time are separate data entry fields but for the model it is one Property), I have to perform some checks and some manual data munging.

The end result of this is that I have some pretty tight ties between the View and Binder. I’m architecturally fine with this but from a code maintenance standpoint, it’s a nightmare.

For example, my model I’m binding here is of type Log (this is the object I will get as a parameter on my Action). The “ServiceStateTime” is a property on Log. The form values of “log.ServiceStartDate” and “log.ServiceStartTime” are totally arbitrary and come from two textboxes on the form (Html.TextBox(“log.ServiceStartTime”,…))

protected override object GetPropertyValue(ControllerContext controllerContext,
                                               ModelBindingContext bindingContext,
                                               PropertyDescriptor propertyDescriptor,
                                               IModelBinder propertyBinder)
{
        if (propertyDescriptor.Name == "ServiceStartTime")
        {
            string date = bindingContext.ValueProvider.GetValue("log.ServiceStartDate").ConvertTo(typeof (string)) as string;
            string time =
                bindingContext.ValueProvider.GetValue("log.ServiceStartTime").ConvertTo(typeof (string)) as string;
            DateTime dateTime = DateTime.Parse(date + " " + time);
            return dateTime;
        }
        if (propertyDescriptor.Name == "ServiceEndTime")
        {
            string date = bindingContext.ValueProvider.GetValue("log.ServiceEndDate").ConvertTo(typeof(string)) as string;
            string time =
                bindingContext.ValueProvider.GetValue("log.ServiceEndTime").ConvertTo(typeof(string)) as string;
            DateTime dateTime = DateTime.Parse(date + " " + time);
            return dateTime;
        }

The Log.ServiceEndTime is a similar field.

This doesn’t feel very DRY to me. First, if I refactor the ServiceStartTime or ServiceEndTime into different field names, the text strings may get missed (although my refactoring tool of choice, R#, is pretty good at this sort of thing, it wouldn’t cause a build-time failure and would only get caught by manual testing). Second, if I decided to arbitrarily change the descriptors “log.ServiceStartDate” and “log.ServiceStartTime”, I would run into the same problem. To me, runtime silent errors are the worst kind of error out there.

So, I see a couple of options to help here and would love to get some input from people who have come across some of these issues:

  • Refactor any text strings in common between the view and model binders out into const strings attached to the ViewModel object I pass from controller to the aspx/ascx view. This pollutes the ViewModel object, though.
  • Provide unit tests around all of the interactions. I’m a big proponent of unit tests and haven’t started fleshing this option out but I’ve got a gut feeling that it won’t save me from foot-shootings.

If it matters, the Log and other entities in the system are persisted to the database using Fluent NHibernate. I really want to keep my controllers as thin as possible.

So, any suggestions here are greatly welcomed!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:41:46+00:00Added an answer on May 13, 2026 at 10:41 pm

    You can use ViewModel:

    class ViewModelClass 
    {
        [DateValidationAttribute]
        public property DateTime ServiceStartTimeDate { get; set; }
        [TimeValidationAttribute]
        public property DateTime ServiceStartTimeTime { get; set; }
    }
    

    DefaultModelBinder binder doesn’t need any modification to bind these fields. Then you can write function to combine these fields:

    class DateUtil
    {
         public static DateTime CombineDateAndTime(DateTime Date, DateTime Time);
    }
    

    Then you get entity from database:

    var entity = context.GetUpdatedEntity(id);
    entity.ServiceStartTime = DateUtil.CombineDateAndTime(viewModel.ServiceStartTimeDate,viewModel.ServiceStartTimeTime);
    

    If you really want to have it in model binder, you can do it like this:

    public class DateAndTimeModelBinder : DefaultModelBinder
    {
        protected override object GetPropertyValue(ControllerContext controllerContext,
                                               ModelBindingContext bindingContext,
                                               PropertyDescriptor propertyDescriptor,
                                               IModelBinder propertyBinder)
        {
            if (propertyDescriptor.PropertyType == typeof(DateTime))
            {
                string time = bindingContext.ValueProvider.GetValue(CreateSubPropertyName(bindingContext.ModelName, propertyDescriptor.Name + "Time")).ConvertTo(typeof(string)) as string;
                var date = base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
                if ((date != null) && (time != null))
                    return CombineDateAndTime(date, time);
            }
            return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
        }
    }
    

    When DateTime field is being bound, binder looks if there is additional form field with “Time” at the end (if we bind “ServiceStartTime”, it look for “ServiceStartTimeTime”, so you bind date portion to “ServiceStartTime” field and time portion to “”ServiceStartTimeTime”). If there is, it adds its value to date. You write it once and will work for every field. Code above will surely not work, it needs some adjustments, but shows idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.