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

  • Home
  • SEARCH
  • 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 873035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:52:20+00:00 2026-05-15T10:52:20+00:00

I have found myself with at little problem, and I think a custom model

  • 0

I have found myself with at little problem, and I think a custom model binder is the way to go.

My Domain model looks like this,readly standard
I got a Page and a Template. The Page has the Template as a ref.
So the Default asp.net mvc Binder, does not know how to bind it, therefore I need to make some rules for it. (Custom Model Binder)

public class PageTemplate
{
    public virtual string Title { get; set; }
    public virtual string Content { get; set; }
    public virtual DateTime? Created { get; set; }
    public virtual DateTime? Modified { get; set; }
}



public class Page
{
    public virtual string Title { get; set; }
    public virtual PageTemplate Template { get; set; }
    public virtual string Content { get; set; }
    public virtual DateTime? Created { get; set; }
    public virtual DateTime? Modified { get; set; }
}

So I have Registreted the ModelBinder in globals.asax

ModelBinders.Binders.Add(typeof(Cms.Domain.Entities.Page),
                        new Cms.UI.Web.App.ModelBinders.PageModelBinder(
                                new Services.GenericApplicationService<Cms.Domain.Entities.Page>().GetEntityStore()
                            )
                        );

My ModelBinder tage a paremeter, witch is my Repository, where I get all my Entities ( Page, Template )

My Controller for a Page looks like this.
I have posted into a Create Controler, it does not matter for now, if it was a Update method.
Since I in this case have a dropdown, that represents the Template, I will get an ID in my form collection.

I then call: TryUpdateModel and I got a hit in my PageModelBinder.

[AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken]
[ValidateInput(false)]
public ActionResult Create(FormCollection form)
{
    Page o = new Page();

    string[] exclude = new { "Id" }
    if (base.TryUpdateModel<Page>(o, string.Empty, null, exclude, form.ToValueProvider()))
    {
        if (ModelState.IsValid)
        {
            this.PageService.Add(o);
            this.CmsViewData.PageList = this.PageService.List();
            this.CmsViewData.Messages.AddMessage("Page is updated.", MessageTypes.Succes);
            return View("List", this.CmsViewData);
        }
    }

    return View("New", this.CmsViewData);
}

So I end op with the Model Binder.
I have search the internet dry for information, but im stock.

I need to get the ID from the FormCollection, and parse it to at Model from my IEntityStore.
But how ?

public class PageModelBinder : IModelBinder
{

    public readonly IEntityStore RepositoryResolver;

    public PageModelBinder(IEntityStore repositoryResolver)
    {
        this.RepositoryResolver = repositoryResolver;
    }

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }

        if (modelType == typeof(Cms.Domain.Entities.Page))
        {
            // Do some magic 
            // Get the Id from Property and bind it to model, how ??
        }
    }

}

// Dennis

I hope, my problom is clear.

  • 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-15T10:52:20+00:00Added an answer on May 15, 2026 at 10:52 am

    Did find a work around.
    I download the sourcecode for asp.net r2 rtm 2

    And did copy all code for the default ModelBinder, and code it need. Did some minor change, small hacks.

    the work around is doing a little hack in this method:

    [SuppressMessage("Microsoft.Globalization", "CA1304:SpecifyCultureInfo", MessageId = "System.Web.Mvc.ValueProviderResult.ConvertTo(System.Type)",
            Justification = "The target object should make the correct culture determination, not this method.")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes",
            Justification = "We're recording this exception so that we can act on it later.")]
        private static object ConvertProviderResult(ModelStateDictionary modelState, string modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)
        {
            try
            {
                object convertedValue = valueProviderResult.ConvertTo(destinationType);
                return convertedValue;
            }
            catch (Exception ex)
            {
                try
                {
                    // HACK if the binder still fails, try get the entity in db.
                    Services.GenericApplicationService<Cms.Domain.Entities.PageTemplate> repo;
                    repo = new Services.GenericApplicationService<Cms.Domain.Entities.PageTemplate>();
                    int id = Convert.ToInt32(valueProviderResult.AttemptedValue);
                    object convertedValue = repo.Retrieve(id);
                    return convertedValue;
                }
                catch (Exception ex1)
                {
                    modelState.AddModelError(modelStateKey, ex1);
                    return null;
                }
            }
        }
    

    This question is closed.

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

Sidebar

Related Questions

While fleshing out a hypothetical domain model, I have found myself wondering whether the
I have made this mistake several times, and found myself referencing This Post every
I'm currently working on project with Haskell, and have found myself some trouble. I'm
Recently, I found myself having to write up some concerns I have about race
I have recently found myself on a Linux computer and am liking it so
I'm using pylons with sqlalchemy. I have several models, and found myself wrote such
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found that my HTML is, to be honest, very clunky. Small, simple
I have found some libraries or web services in PHP that does the job.
I have found jQuery to be a great tool to simplify my MVC Views.

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.