0

I have searched SO for similar posts and have not been successful with any given solutions. So I have posted this again.

I am trying to post some information back to the database in this Controller Action. This Action is called when Save button is clicked on the View.

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult EditDetail(int id, FormCollection fvals)
    {
        Environments env = environmentsRepository.GetEnvironmentDetail(id);
        UpdateModel(env);
        environmentsRepository.Save();
        return RedirectToAction("List", new RouteValueDictionary(new 
        {controller="Environments", action="List", id = 1}));  
    }

When I click on the Save button, I get the following error:

Line 32:
Line 33:
Line 34: <% foreach (var environment in Model) <--Error in this line
Line 35: { %>
Line 36:

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP.views_environments_list_aspx.__RenderContent2(HtmlTextWriter __w, Control     parameterContainer) in e:\code\ossp\WebUI\Views\Environments\List.aspx:34

Please let me know what I am missing? How do I resolve the error?

List View: http://pastebin.ubuntu.com/544767/
List Action: http://pastebin.ubuntu.com/544768/

Resolution:
I have realized that I was passing the wrong parameter 'id' instead of 'page', to the List view.

Thanks everyone for their pointers. I was able to learn a lot due to this discussion. Unfortunately, I am still at 11 to be able to give some votes. Thanks!

5
  • please post the code for the List action from the Environment controller Commented Dec 17, 2010 at 6:14
  • Ahmad, I have posted the link to List Action and View code to the post. Commented Dec 17, 2010 at 6:21
  • 1
    maybe the TODO needs to be done return View(); ///TODO Handle This. in this case the model is empty Commented Dec 17, 2010 at 6:26
  • a question, how do you declare a property named as here (environment.as). your code shouldn't be compiled! Commented Dec 17, 2010 at 6:30
  • @jani.the property was actually environment.AutoStart. I have just shortened it for here. :-) @ahmad, when i am debug mode, i can see that "env" is getting populated with updated values, so i am under the impression that model is not empty. Commented Dec 17, 2010 at 6:40

2 Answers 2

2

The problem is that inside your EditDetail at the end you are redirecting to the List action of the Environments controller and I suppose that this action renders a strongly typed view. The problem is that the model you passed to the view was null. So your controller action might look something like this:

public ActionResult List(int id)
{
    var model = FetchModelFromSomewhere(id); // this probably returns null here
    return View(model);
}

And inside your view you are trying to enumerate over the model which is null:

<% foreach (var environment in Model) { %>

and you are getting the exception. So make sure that the model you are passing to the List view is not null.

Sign up to request clarification or add additional context in comments.

Comments

1

"Page" does not have a value, so as Ahmad pointed out it looks like you are returning back an empty View as per the ELSE

if (page.HasValue)
    return View(environmentsRepository.Environments.Skip(((int)page - 1) * PageSize).Take(PageSize).ToList());
else
    return View(); ///TODO Handle This

Have you tried debugging through to see what flow the code takes?

2 Comments

I have found that the "page" does indeed return a null value. I am not able to understand why the List controller is able to display content when the navigating through page links, but when it is being called from the redirectToAction in EditDetail, page is being returned as null. Any idea why?
I have realized that I was passing the wrong parameter 'id' instead of 'page', to the List view. I changed this and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.