MVC - I keep getting the following error message: Object reference not set to an instance of an object

Head48
Head48 used Ask the Experts™
on
I keep getting the following error message 'Object reference not set to an instance of an object' can someone please tell me what I am doing wrong. Code and error message is attached. Thanks Northwind-Code.txt  Mvc - Error Message
CONTROLLER
----------

---------------------------------------[ ProfilesController ]-------------------------------------

using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Mvc_Northwind.Models;

namespace Mvc_Northwind.Controllers
{
    public class ProfilesController : Controller
    {
        //
        // GET: /Profiles/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Edit()
        {
            Profiles profiles = new Profiles();
            profiles.FirstName = "Headley";
            profiles.City = "Birmingham";
            ViewData["profiles"] = profiles;
            return View();
        }

    }
}

MODELS
------

-----------------------------------------------[ PROFILES.CS ]-----------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc_Northwind.Models
{
    public class Profiles
    {
        public string FirstName { get; set; }
        public string City { get; set; }
    }
}


VIEWS
-----

-----------------------------------------------[ EDIT.ASPX ]-------------------------------------------------

<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
    
    <p><%= Html.Encode(ViewData["Message"]) %> </p>
    <% using (Html.BeginForm()) {%>

        <fieldset>
            <legend>Fields</legend>
            <p>
                <label for="FirstName">FirstName:</label>
                <%= Html.TextBox("FirstName", ViewData.Model.FirstName) %>
                <%= Html.ValidationMessage("FirstName", "*") %>
            </p>
            <p>
                <label for="City">City:</label>
                <%= Html.TextBox("City", ViewData.Model.City) %>
                <%= Html.ValidationMessage("City", "*") %>
            </p>
            <p>
                <input type="submit" value="Save" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>


ERROR MESSAGE

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
I'm new to MVC, but looking at an app I have working, the difference is how you're setting the view data, versus retrieving it.

When I set mine in the controller, I do so like this:

ViewData.Model = MyObject

Then, I can access it in the view like this:

ViewData.Model.MyProperty

You're assigning your data to the property bag, under the key "Profiles", but trying to access it through the Model property.

Just assign it to the Model property, instead of by using a key and you'll be all set.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial