Solved

ASP>NET MVC - Error "Object reference not set to an instance of an object." when using  Html.BuildUrlFromExpression

Posted on 2009-05-20
2
1,983 Views
Last Modified: 2013-12-17
Hi,
I am trying to build a ASP.Net MVC based e-commerce web site.
I have downloaded a sample MVC Storefront App by Rob Conery and copied much of logic from there.

But when I am using  Html.BuildUrlFromExpression to create href for a product image I am getting Object reference not set to an instance of an object error.

If I am commenting out the part with the image - rest works OK and I can see product data . Click on product name Invoke Show action correctly...

Below is my relevant code...
What I am doing wrong????
// ProductSummaryDisplay.ascx

 

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Product>" %>

<%@ Import Namespace="Microsoft.Web.Mvc" %>

<%@ Import Namespace="ClassMedMVC.Controllers" %>

<%@ Import Namespace="ClassMed.Data.Model" %>

<div class="prod-wrap">

    <div class="prod-img">

        <%--Error on the next line: Object reference not set to an instance of an object--%>

        <a href="<%= Html.BuildUrlFromExpression<CatalogController> (x=>x.Show(ViewData.Model.Id)) %>">

            <img src="/Content/ProductImages/<%=ViewData.Model.Image %>" alt="<%=ViewData.Model.Name%> " />

        </a>

    </div>

    <div class="prod-info">

        <h3>

            <%= Html.ActionLink(ViewData.Model.Name, "Show", new {  id=Model.Id  })%>

        </h3>

        <div class="prod-desc">

            <p>

                <%= ViewData.Model.Description %>

            </p>

        </div>

 

    </div>

</div>

 

 

// CatalogControler (partially)

namespace ClassMedMVC.Controllers

{

    public class CatalogController : Controller

    {

 

        public ActionResult Show (int id)

        {

            var rep = MySqlCatalogRepository.Instance;

            Product product = rep.GetProductById(id);

            return View("Show", product);

        }

 

      }

  }

Open in new window

0
Comment
Question by:Michael_D
2 Comments
 
LVL 5

Expert Comment

by:johnaryan
Comment Utility
Looks like you're missing the reference from the top of the file...
<%@ Import Namespace="Helpers" %>
0
 
LVL 13

Accepted Solution

by:
Michael_D earned 0 total points
Comment Utility
I actually went away from the way Rob Conery used in his sample project to display images.
I am actually ended up using following code instead:

<img src="<%= Url.Content("~/Content/ProductImages/" + Model.Image) %>" alt="<%= Html.AttributeEncode(Model.Name) %> " /> 

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now