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 7188029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:58:39+00:00 2026-05-28T18:58:39+00:00

I got a model like this: public class MainModel { public string Id {get;set;}

  • 0

I got a model like this:

public class MainModel
{
   public string Id {get;set;}
   public string Title {get;set;}
   public TimePicker TimePickerField {get;set;}
}

TimePicker is an inner model which looks like this:

public class TimePicker 
{
   public TimeSpan {get;set;}
   public AmPmEnum AmPm {get;set;}
}

I’m trying to create a custom model binding for inner model: TimePicker

The question is: How do I get values in custom model binder which was submitted in form into TimePicker model fields?

If I try to get it like this:

var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

I just get null in value.

I’m not sure how to implement the model binder correctly.

public class TimePickerModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }
        var result = new TimePicker();

        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (value != null)
        {
            bindingContext.ModelState.SetModelValue(bindingContext.ModelName, value);
            try
            {
                //result = Duration.Parse(value.AttemptedValue);
            }
            catch (Exception ex)
            {
               bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex.Message);
            }
        }    

        return result;
    }
}
  • 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-28T18:58:40+00:00Added an answer on May 28, 2026 at 6:58 pm

    The following works for me.

    Model:

    public enum AmPmEnum
    {
        Am, 
        Pm
    }
    
    public class TimePicker 
    {
        public TimeSpan Time { get; set; }
        public AmPmEnum AmPm { get; set; }
    }
    
    public class MainModel
    {
        public TimePicker TimePickerField { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var model = new MainModel
            {
                TimePickerField = new TimePicker
                {
                    Time = TimeSpan.FromHours(1),
                    AmPm = AmPmEnum.Pm
                }
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MainModel model)
        {
            return View(model);
        }
    }
    

    View (~/Views/Home/Index.cshtml):

    @model MainModel
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.TimePickerField)
        <button type="submit">OK</button>
    }
    

    Custom editor template (~/Views/Shared/EditorTemplates/TimePicker.cshtml) which merges the Time and AmPm properties into a single input field and which will require a custom model binder later in order to split them when the form is submitted:

    @model TimePicker
    @Html.TextBox("_picker_", string.Format("{0} {1}", Model.Time, Model.AmPm))
    

    and the model binder:

    public class TimePickerModelBinder : IModelBinder
    {
        public object BindModel(
            ControllerContext controllerContext, 
            ModelBindingContext bindingContext
        )
        {
            var key = bindingContext.ModelName + "._picker_";
            var value = bindingContext.ValueProvider.GetValue(key);
            if (value == null)
            {
                return null;
            }
    
            var result = new TimePicker();
    
            try
            {
                // TODO: instead of hardcoding do your parsing
                // from value.AttemptedValue which will contain the string
                // that was entered by the user
                return new TimePicker
                {
                    Time = TimeSpan.FromHours(2),
                    AmPm = AmPmEnum.Pm
                };
            }
            catch (Exception ex)
            {
                bindingContext.ModelState.AddModelError(
                    bindingContext.ModelName, 
                    ex.Message
                );
                // This is important in order to preserve the original user
                // input in case of error when redisplaying the view
                bindingContext.ModelState.SetModelValue(key, value);
            }
            return result;
        }
    }
    

    and finally register your model binder in Application_Start:

    ModelBinders.Binders.Add(typeof(TimePicker), new TimePickerModelBinder());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a set of models that look like this: class Page(models.Model): title =
Say I've got a domain model created from C# classes like this: public class
I've got a model like this: public class ParentViewModel { public class ChildViewModel {
I've got class named PagedList which looks like this: public class PagedList<T> : List<T>
I've got a view model like this: public class SignUpViewModel { [Required(ErrorMessage = Bitte
I've got some generic class for my JPA model POJO that goes like this:
I've got my model which contains some members: public class Address { public Street
I've got a model like this def upload_location(instance, filename): return 'validate/%s/builds/%s' % (get_current_user(), filename)
I've got a model with 3 properties that are of type KeyValuePair public class
I got a controller action like public class Question { public int Id {

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.