Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2143
  • Last Modified:

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

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
Michael_D
Asked:
Michael_D
1 Solution
 
johnaryanCommented:
Looks like you're missing the reference from the top of the file...
<%@ Import Namespace="Helpers" %>
0
 
Michael_DAuthor Commented:
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now