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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:11:20+00:00 2026-05-30T08:11:20+00:00

I have the following code: [HttpGet] public ActionResult Edit(int req) { var viewModel =

  • 0

I have the following code:

[HttpGet]
public ActionResult Edit(int req)
{
    var viewModel = new  EditViewModel();
    viewModel.RequestId = int;
    return View(viewModel);
}

[HttpPost]
Public ActionResult Edit(EditViewModel viewModel)
{
// some code here...
}

It works fine: when the edit form is posted, I have the action controller who is called.

Now I modify some little bit my code like this:

[HttpGet]
public ActionResult Edit(int req)
{
    var viewModel = new  EditViewModel(req);
    return View(viewModel);
}

[HttpPost]
Public ActionResult Edit(EditViewModel viewModel)
{
// some code here...
}

public class EditViewModel()
{
    public EditViewModel(int req)
    {
        requestId = req; 
    }
    ...
}

In this new version, I have a view model with a contructor.

This time, when my form is posted back, the action controller is never triggered.

Any idea?

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-30T08:11:22+00:00Added an answer on May 30, 2026 at 8:11 am

    That’s normal. The default model binder can no longer instantiate your view model as it doesn’t have a parameterless constructor. You will have to write a custom model binder if you want to use view models that don’t have a default constructor.

    Normally you don’t need such custom constructor. You could simply have your view model like that:

    public class EditViewModel()
    {
        public int RequestId { get; set; }
    }
    

    and the POST action like that:

    [HttpPost]
    public ActionResult Edit(EditViewModel viewModel)
    {
        // some code here...
    }
    

    and now all you have to do is POST the requestId parameter instead of req and the default model binder will do the job.

    And if for some reason you wanted to use a view model with custom constructor, here’s an example of how the custom model binder might look like:

    public class EditViewModelBinder : DefaultModelBinder
    {
        protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
        {
            var req = bindingContext.ValueProvider.GetValue("req");
            if (req == null)
            {
                throw new Exception("missing req parameter");
            }
            int reqValue;
            if (!int.TryParse(req.AttemptedValue, out reqValue))
            {
                throw new Exception(string.Format("The req parameter contains an invalid value: {0}", req.AttemptedValue));
            }
    
            return new EditViewModel(reqValue);
        }
    }
    

    which will be registered in your Application_Start:

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

Sidebar

Related Questions

I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code: HttpGet httpGet = new HttpGet(serverAdress + /rootservices); httpGet.setHeader(Accept, text/xml);
I have following code class User attr_accessor :name end u = User.new u.name =
I have following code: public static void ProcessStep(Action action) { //do something here if
Have the following code: if (post) { HttpPost request = new HttpPost(url); Log.i(TAG, Posting
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in
I have following code in my Application. Comments in my code will specify My
I have following code in my application. [self.navigationController pushViewController:x animated:YES]; It will push a
i have following code to show div on page <div id=all_users> <div id=user11 userid=11

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.