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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:25:38+00:00 2026-05-31T17:25:38+00:00

I have a simple MVC 4 (Beta) Application and just observed something that I

  • 0

I have a simple MVC 4 (Beta) Application and just observed something that I assume may be handled differently. In this case, when the below Product Action is called a Model is created and assigned to ViewData for use in the View. This all works fine, the Model property is not-null, and the Views use of the Model property works.

[HttpGet]
public ActionResult Product()
{
    return ExecuteFormRequest(View(), true, delegate
    {
        var model = Request.ParseFromQueryString<Product>();

        if (model != null)
        {
            ViewData.Model = model;
        }

        return View();
    });
}

When the Product is updated and Post’ed back to the Controller all the data looks fine, but if the product fails validation and I return the View ActionResult (as shown below) the View encounters a null reference exception because the Views Model property is now null

[HttpPost]
public ActionResult Product(Product product)
{
    if (!ModelState.IsValid)
    {
        return View();
    }

    // Do other business stuff and target new or same View    

    return View();
}

This is the line in the View that leads to a null reference exception, because the Model Property is now null.

@if (!string.IsNullOrEmpty(Model.MyValue)) {}

The way I resolved this was to reset the Model Property (as shown below). This seems wrong to me and I’m wondering if I’m approaching this all wrong. Any ideas welcome?

[HttpPost]
public ActionResult Product(Product product)
{
    if (product!= null)
    {
        ViewData.Model = product;
    }

    if (!ModelState.IsValid)
    {
        return View();
    }

    // Do other business stuff and target new or same View    

    return View();
}
  • 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-31T17:25:40+00:00Added an answer on May 31, 2026 at 5:25 pm

    If you have a strongly typed viewmodel, why are you using viewdata? try this:

    [HttpGet]
    public ActionResult Product()
    {
        return ExecuteFormRequest(View(), true, delegate
        {
            var model = Request.ParseFromQueryString<Product>();
    
            //if (model != null)
            //{
            //    ViewData.Model = model;
            //}
    
            //return View();
            return View(model);
        });
    }
    
    [HttpPost]
    public ActionResult Product(Product product)
    {
        if (!ModelState.IsValid)
        {
            //return View();
            return View(product);
        }
    
        // Do other business stuff and target new or same View    
    
        //return View();
        return View(product);
    }
    

    The reason you are seeing the behavior with your approach is because HTTP is stateless. Unlike webforms, MVC does not reconstitute all of your variables after a post. Each action method starts out with a new ViewData dictionary.

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

Sidebar

Related Questions

I have a simple form on an ASP.NET MVC site that I'm building. This
I have a simple class in asp.net mvc that looks like this: public class
I have a simple Spring MVC application that looks up some user details from
I have a simple MVC application that sometimes fails in production. I would like
I have a simple application with MVC pages that have no javascript, images or
I have successfully setup a simple mvc application that lists teams. I'm using Ninject
I made a really simple MVC application with VS 2010 beta. just a view
I have this simple MVC 3 application which simply has two controllers(Home and Edit),
I have written a VERY simple MVC application which just displays a single string
I have a simple ASP.NET MVC web application that uses NHibernate with FluentNHibernate's auto

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.