Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Search | Today's Posts | Mark Forums Read
BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950
This is the forum to discuss the Wrox book ASP.NET MVC Website Programming Problem Design Solution by Nicholas Berardi, Al Katawazi, and Marco Bellinaso; ISBN: 9780470410950
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET MVC Website Programming Problem Design Solution ISBN: 9780470410950 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 26th, 2009, 07:15 AM
Authorized User
 
Join Date: Mar 2007
Location: , , Denmark.
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
Default System.NullReferenceException in the EditUser View...

I have a strange problem in the ManageUser/EditUser part of the site.
I'm building the site from scratch, using a different layout, but so far, the Controller and Model code is identical with the original site. The code in the EditUser and ManageUser Views is 100% identical.

When I press the "Edit user" link on the ManageUser View, I get the
"System.NullReferenceException: Object reference not set to an instance of an object" error in the EditUser View.

I ran the debugger, and I can see that, when I press the ""Edit user" link on the ManageUser View, the EditUser Action is called, but the "id" parameter is empty, which means the MembershipUser object will be null. So, the Model which is passed to the EditUser View is null, and the Veiw can't write any values from the Model, hence the NullReferenceException.

When I hover the mouse over the EditUser link, I can see the correct path, with the correct querystring parameter, in the status line in the lower left corner of the browser:

http://localhost:port#/User/EditUser?id=Admin

In the EditUser Action, if I hardcode the id parameter to ex. "Admin", like this:

Code:
[Authorize(Roles = "Admin")]
        public ActionResult EditUser(string id)
        {
            id = "Admin";
            ViewData["roles"] = (String[])Roles.GetAllRoles();
            MembershipUser membershipUser = Membership.GetUser(id);

            ViewData["PageTitle"] = "Edit " + id;
            return View(membershipUser);
        }
- it works, and the EditUser View is displayed correctly.

So, it seems like the querystring parameter isn't passed to the EditUser Action.
I'm running the site using Cassini, but the original site runs perfectly on Cassini, so this shouldn't be the reason...

Any thoughts?

Last edited by philthy; July 26th, 2009 at 07:24 AM..
  #2 (permalink)  
Old July 26th, 2009, 07:29 AM
Wrox Author
 
Join Date: Aug 2008
Location: Pittsford, NY, USA.
Posts: 19
Thanks: 1
Thanked 4 Times in 4 Posts
Send a message via AIM to Al_Katawazi
Default

Alright it definately seems like the param is not getting in. Send us exactly what the HTML looks like in your view that creates that link. It's probably a miss spelling somewhre between the view and the controller
  #3 (permalink)  
Old July 26th, 2009, 08:07 AM
Authorized User
 
Join Date: Mar 2007
Location: , , Denmark.
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
Default

This is the entire ManageUser View. As you can see, the only difference between this and the original is that, because I use a different layout, I have a few more Content areas.


HTML Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Security.MembershipUserCollection>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">

    <% Html.RenderPartial("~/Views/Shared/Message.ascx"); %>
    <form method="post" action="ManageUser" class="manage-user">
    <p>
        <b>Total Registered Users: </b>
        <%= ViewData["RegisteredUsers"]%></p>
    <p>
        <b>Users Online Now: </b>
        <%= ViewData["UsersOnlineNow"] %></p>
    <hr />
    <!-- user search options -->
    <p>
        Search Members:
        <%= Html.TextBox("searchInput",ViewData["searchInput"])%>&nbsp;&nbsp;
        <%= Html.DropDownList("searchType", (SelectList)ViewData["searchOptionList"])%>&nbsp;&nbsp;
        <button type="submit" id="user-manageUser-button" style="height: 28px">
            Search</button></p>
    <hr />
    <!-- the user grid -->
    <table cellpadding="2" cellspacing="0" align="left" summary="User Grid" border="1">
        <thead>
            <tr style="font-weight: bold; background-color: #A8C3CB;">
                <th align="center">
                    Username
                </th>
                <th align="center">
                    E-Mail
                </th>
                <th align="center">
                    Created
                </th>
                <th align="center">
                    Last Used
                </th>
                <th align="center">
                    Approved
                </th>
                <th>
                    &nbsp;
                </th>
                <th>
                    &nbsp;
                </th>
            </tr>
        </thead>
        <tbody>
            <% foreach(MembershipUser membershipUser in Model) { %>
            <tr id="user-<%= membershipUser.UserName %>">
                <td>
                    <%= Html.Encode(membershipUser.UserName) %>
                </td>
                <td>
                    <%= Html.Encode(membershipUser.Email) %>
                </td>
                <td align="center">
                    <%= membershipUser.CreationDate.ToLocalTime() %>
                </td>
                <td align="center">
                    <%= membershipUser.LastActivityDate.ToLocalTime() %>
                </td>
                <td align="center">
                    <%= Html.CheckBox("approved" ,membershipUser.IsApproved, new {disabled="true"}) %>
                </td>
                <td>
                    <a href="EditUser?id=<%= membershipUser.UserName %>">
                        <img border="0" alt="Edit User" src="/Content/images/EditSymbol.png" title="Modify User"
                            align="middle" /></a>
                </td>
                <td align="center">
                    <a class="delete-user-button" href="#" meta:id="<%= membershipUser.UserName %>">
                        <img border="0" alt="Delete User" src="/Content/images/DeleteSymbol.png" title="Delete User"
                            align="middle" /></a>
                </td>
            </tr>
            <% } %>
        </tbody>
    </table>
    </form>
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="Secondary" runat="server">
</asp:Content>
<asp:Content ID="ScriptContent" ContentPlaceHolderID="ScriptContent" runat="server">

    <script type="text/javascript" src="/Content/scripts/manage-users.js"></script>

</asp:Content>
This is the code in the EditUser View:

HTML Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<System.Web.Security.MembershipUser>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="Content" runat="server">
    <% Html.RenderPartial("~/Views/Shared/Message.ascx"); %>
    <form method="post" action="EditUser" class="manage-user">
    <!-- The hidden control manages the username -->
    <%= Html.Hidden("id") %>
    
    <p>
        <b>UserName:</b><%= Html.Encode(Model.UserName) %>
    </p>
    <p>
        <b>E-Mail:</b>
        <%= Html.Encode(Model.Email) %></p>
    <p>
        <b>Registered:</b>
        <%= Model.CreationDate.ToLocalTime()%></p>
    <p>
        <b>Last Login:</b>
        <%= Model.LastLoginDate.ToLocalTime()%></p>
    <p>
        <b>Last Activity:</b>
        <%= Model.LastActivityDate.ToLocalTime()%></p>
    <p>
        <b>Online Now:</b>
        <%= Html.CheckBox("onlineNow", Model.IsOnline, new { disabled = "true" })%></p>
    <p>
        <b>Approved:</b>
        <%= Html.CheckBox("approved", Model.IsApproved)%></p>
    <p>
        <b>Locked Out:</b>
        <%= Html.CheckBox("lockedOut", Model.IsLockedOut, new { disabled = "true" })%></p>
    <hr />
    <!-- This portion allows you to actually edit the roles of the user -->
    <h2>
        Edit User Roles</h2>
    <ul>
        <% foreach (string role in (string[])ViewData["roles"])
           { %>
        <li>
            <%= Html.CheckBox("role." + role,  Roles.IsUserInRole(Model.UserName, role))%>
            <label for="role.<%= role %>">
                <%= Html.Encode(role) %></label></li>
        <% } %>
    </ul>
    <p>
        <button type="submit" id="user-editUser-button">
            Update User</button>
        <button type="button" onclick="location.href='/User/ManageUser'" style="margin-left: 2em;">
            Return</button>
    </p>
    </form>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Secondary" runat="server">
</asp:Content>
<asp:Content ID="ScriptContent" ContentPlaceHolderID="ScriptContent" runat="server">
</asp:Content>
This is how the links are generated (View source):
HTML Code:
<td>
  <a href="EditUser?id=user">
     <img border="0" alt="Edit User" src="/Content/images/EditSymbol.png" title="Modify User"
     align="middle" /></a>
</td>

Last edited by philthy; July 26th, 2009 at 08:10 AM..
  #4 (permalink)  
Old July 26th, 2009, 09:21 AM
Wrox Author
 
Join Date: Aug 2008
Location: Pittsford, NY, USA.
Posts: 19
Thanks: 1
Thanked 4 Times in 4 Posts
Send a message via AIM to Al_Katawazi
Default

Try changing the ?id= to / in the link and see if that works for you. Otherwise if that doesn't work send the first couple of lines of the controller code that you are using.
The Following User Says Thank You to Al_Katawazi For This Useful Post:
philthy (July 26th, 2009)
  #5 (permalink)  
Old July 26th, 2009, 10:36 AM
Authorized User
 
Join Date: Mar 2007
Location: , , Denmark.
Posts: 52
Thanks: 6
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Al_Katawazi View Post
Try changing the ?id= to / in the link and see if that works for you. Otherwise if that doesn't work send the first couple of lines of the controller code that you are using.
Yup, changing the ?id= to / in the link works...

Now that I think of it, isn't this the way it is supposed to look in MVC, since the route in Global.asax looks like this:
{controller}/{action}/{id}

Strange that it works in the original project, without this change...

Anyway, thanks for your help
  #6 (permalink)  
Old July 26th, 2009, 03:22 PM
Wrox Author
 
Join Date: Aug 2008
Location: Pittsford, NY, USA.
Posts: 19
Thanks: 1
Thanked 4 Times in 4 Posts
Send a message via AIM to Al_Katawazi
Default

You are quite right that the slash is the way to go when building the url. I have heard that google disregards anything after a ? in terms of SEO but you never know with google.

Rob Connery did a great piece on setting up routing located here. Sometimes though it is just simpler to slap in url variables. Personal preference.
  #7 (permalink)  
Old July 27th, 2009, 01:19 PM
Authorized User
 
Join Date: Apr 2009
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Default

I had this issue when I first started rebuilding the project, because I included the default home (index and about) pages that are created in visual studio. So the route that was being passed in was the default route and not the admin/user/edit (may not be exact) route. I noticed the issue came up with some other routes that needed a page number or id. So, I moved the default route to the end of the routing system in global.asax file and removed the default id="" and everything works correctly. I wonder if you might be experiencing something similar.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.NullReferenceException JayLou BOOK: Wrox's ASP.NET 2.0 Visual Web Developer 2005 Express Edition Starter ISBN: 978-0-7645-8807-5 1 April 6th, 2007 12:55 PM
System.NullReferenceException m_rajib74 C# 0 March 5th, 2007 04:50 AM
System.NullReferenceException lwheless BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 0 September 24th, 2006 05:47 AM
System.NullReferenceException on WebService Objec ag19702004 General .NET 0 December 23rd, 2005 04:16 PM
System.NullReferenceException: Object reference... nordestgaard All Other Wrox Books 4 January 7th, 2004 01:06 AM



All times are GMT -4. The time now is 02:49 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.