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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:52:28+00:00 2026-05-23T13:52:28+00:00

I have an action that looks like this: [Post] [PopulateModelFromId] public ActionResult ChangeName( string

  • 0

I have an action that looks like this:

[Post]
[PopulateModelFromId]
public ActionResult ChangeName( string name, MyModel model )
{
    try
    {
        model.changeName
        return JSONSuccess();
    }
    catch( ModelUpdateException )
    {
        return JSONFail();
    }
}

The name and model id are sent by an ajax POST, and the model is populated by a custom action filter that takes the id and retrieves the model from the database.

The actionfilter looks like this:

...
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
    // parse the id from the request
    MyModel model = getModelFromDataStoreById( id );
    filterContext.ActionParameters["model"] = model;
}
...

The problem is that the MyModel object doesn’t have a parameterless constructor, and MVC is attempting to create and bind to the MyModel object before the ActionFilter is even called but throws an exception because it cannot instantiate the MyModel object.

My first question is am I doing this properly or should I be using something like HttpContext.Items to transfer data between filter and action? Second, is there a way to tell MVC to not try to bind the MyModel object because it will be created later?

  • 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-23T13:52:29+00:00Added an answer on May 23, 2026 at 1:52 pm

    A custom model binder seems more appropriate for this task than a custom action filter:

    public class MyModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var id = bindingContext.ValueProvider.GetValue("id");
            if (id != null)
            {
                return GetModelFromDataStoreById(id.AttemptedValue);
            }
            return base.CreateModel(controllerContext, bindingContext, modelType);
        }
    }
    

    which yuo would register in Application_Start:

    ModelBinders.Binders.Add(typeof(MyModel), new MyModelBinder());
    

    Now your controller action might look like this:

    [HttpPost]
    public ActionResult ChangeName(string name, MyModel model)
    {
        try
        {
            model.ChangeName();
            return JSONSuccess();
        }
        catch (ModelUpdateException)
        {
            return JSONFail();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi, I have a action that looks like this : [AcceptVerbs(HttpVerbs.Post)] public ActionResult Register(AdRegister
I have an action method that looks like this: public ActionResult DoSomething(string par, IEnumerable<string>
I'm perplexed by this. I have a login action that looks like this: public
i have ajax function that looks like this: jQuery.ajax({ type: 'POST', data: { action:
I have some jQuery that looks like this: $.ajax({ type: POST, url: /Customer/CancelSubscription/<%= Model.Customer.Id
I have a form in my index.html that looks like this: <form name=form method=post
I have an ajax call that looks like this: $.ajax({ url: '@Url.Action(SaveStudy)', type: POST,
I have a simple Edit action in ASP.NET MVC that looks like this :
In ASP.NET MVC I have a controller that looks somehow like this: public class
I have a route that looks like this: new { controller = ControllerName, action

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.