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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:02:55+00:00 2026-05-10T21:02:55+00:00

I have been working my way through Scott Guthrie’s excellent post on ASP.NET MVC

  • 0

I have been working my way through Scott Guthrie’s excellent post on ASP.NET MVC Beta 1. In it he shows the improvements made to the UpdateModel method and how they improve unit testing. I have recreated a similar project however anytime I run a UnitTest that contains a call to UpdateModel I receive an ArgumentNullException naming the controllerContext parameter.

Here’s the relevant bits, starting with my model:

public class Country {   public Int32 ID { get; set; }   public String Name { get; set; }   public String Iso3166 { get; set; } } 

The controller action:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Int32 id, FormCollection form) {   using ( ModelBindingDataContext db = new ModelBindingDataContext() ) {     Country country = db.Countries.Where(c => c.CountryID == id).SingleOrDefault();      try {       UpdateModel(country, form);        db.SubmitChanges();        return RedirectToAction('Index');     }     catch {       return View(country);     }   } } 

And finally my unit test that’s failing:

[TestMethod] public void Edit() {   CountryController controller = new CountryController();   FormCollection form = new FormCollection();   form.Add('Name', 'Canada');   form.Add('Iso3166', 'CA');    var result = controller.Edit(2 /*Canada*/, form) as RedirectToRouteResult;    Assert.IsNotNull(result, 'Expected to be redirected on successful POST.');   Assert.AreEqual('Show', result.RouteName, 'Expected to redirect to the View action.'); } 

ArgumentNullException is thrown by the call to UpdateModel with the message ‘Value cannot be null. Parameter name: controllerContext’. I’m assuming that somewhere the UpdateModel requires the System.Web.Mvc.ControllerContext which isn’t present during execution of the test.

I’m also assuming that I’m doing something wrong somewhere and just need to pointed in the right direction.

Help Please!

  • 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. 2026-05-10T21:02:56+00:00Added an answer on May 10, 2026 at 9:02 pm

    I don’t think it can be done since TryUpdateModel, which UpdateModel uses, references the ControllerContext which is null when invoked from a unit test. I use RhinoMocks to mock or stub the various components needed by the controller.

    var routeData = new RouteData(); var httpContext = MockRepository.GenerateStub<HttpContextBase>(); FormCollection formParameters = new FormCollection();  EventController controller = new EventController(); ControllerContext controllerContext =      MockRepository.GenerateStub<ControllerContext>( httpContext,                                                     routeData,                                                     controller ); controller.ControllerContext = controllerContext;  ViewResult result = controller.Create( formParameters ) as ViewResult;  Assert.AreEqual( 'Event', result.Values['controller'] ); Assert.AreEqual( 'Show', result.Values['action'] ); Assert.AreEqual( 0, result.Values['id'] ); 

    Here’s the relevant bit from the Controller.cs source on http://www.codeplex.com/aspnet:

    protected internal bool TryUpdateModel<TModel>( ... ) where TModel : class {       ....      ModelBindingContext bindingContext =            new ModelBindingContext( ControllerContext,                                     valueProvider,                                     typeof(TModel),                                     prefix,                                     () => model,                                     ModelState,                                     propertyFilter );       ... } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to rails and have been working my way through the
I'm new to Ruby, and have been working my way through Mr Neighborly's Humble
I have been working my way through several books on X-code, the Apple document
I have been working my way through the tutorials on learnpython.org and up to
I'm starting out with wxPython and have been working my way through every tutorial
I'm new to android but I've been working my way through the tutoials. Still
I have been working on a project and gotten it through the first stage.
I am new to cakephp and have been working through the Apress book Beginning
I am new to python and have been working through the examples in Swaroop
I've been working on a C# ASP.Net application that requires images to be customized

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.