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
2,002 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
ID: 24466892
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
ID: 24484075
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

770 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