Share via


Include @Html.ActionLink in a @helper

Question

Monday, May 19, 2014 3:06 PM

My app is using MVC 3, razor and EF 5.

I have a helper that displays an image and some information that related to the img (caption, credits, etc. )for some of the reparative code:

@helper CreateImg(string _picAlign, int _PhotoID, string _PhotoRecID, string _PhotoFileType, string _Photographer, string _PhotoDateCreated, int _picCount, string _PhotoCaption, string _PhotoComments)
{

     
             var imgFile = "/Images/Parks/" + _PhotoRecID + "." + _PhotoFileType;           // file name + file Type (jpeg, png, gif etc)
   
   
   <table style="float:@_picAlign; padding-bottom:2em; margin-left:0.2px;">

       <tr style="height:10px"><td style="background-color: white;" colspan=3 class=caption></td></tr>
       <tr><td rowspan=4 style="background-color: white; width:0px"></td>

        <td style="float: none," class=captiontiny>
       
    
            <table style="float: none;" border=0>
            <tr>
                <td style="background-color: white; "></td>

                <td style="text-align:right; float:none;" class=captiontiny>
  
                
                    <img  src="@imgFile" width="275"  />
                    <br />By  @_Photographer, on @_PhotoDateCreated<br />
                     @Html.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" }).ToHtmlString(); }</td>
      
 omitted

Called from the view as:

      @PhotoImg.CreateImg("right", Model.Photo[0].PhotoID, Model.Photo[0].PhotoRecID, Model.Photo[0].PhotoFileType, Model.Photo[0].Photographer, Model.Photo[0].PhotoDateTaken, picCount, Model.Photo[0].PhotoCaption, Model.Photo[0].PhotoComments);

and

      @PhotoImg.CreateImg("right", Model.Photo[i].PhotoID, Model.Photo[i].PhotoRecID, Model.Photo[i].PhotoFileType, Model.Photo[i].Photographer, Model.Photo[i].PhotoDateTaken, picCount, Model.Photo[i].PhotoCaption, Model.Photo[i].PhotoComments);

I am getting the following error message:

Error      1              'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)       c:\MVCProjects\CWBFM\CWBFM\App_Code\PhotoImg.cshtml                28           20           CWBFM

The  @Html.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" }).ToHtmlString(); } works fine if from the view, just not from the helper.

All replies (9)

Tuesday, May 20, 2014 5:30 AM âś…Answered

Hi HooksB,

Please modify your code like this:

@using System.Web.Mvc.Html;
@helper CreateImg(System.Web.Mvc.HtmlHelper h,string _picAlign, int _PhotoID, string _PhotoRecID, string _PhotoFileType, string _Photographer, string _PhotoDateCreated, int _picCount, string _PhotoCaption, string _PhotoComments)
{

     
             var imgFile = "/Images/Parks/" + _PhotoRecID + "." + _PhotoFileType;           // file name + file Type (jpeg, png, gif etc)
   
   
   <table style="float:@_picAlign; padding-bottom:2em; margin-left:0.2px;">

       <tr style="height:10px"><td style="background-color: white;" colspan=3 class=caption></td></tr>
       <tr><td rowspan=4 style="background-color: white; width:0px"></td>

        <td style="float: none," class=captiontiny>
       
    
            <table style="float: none;" border=0>
            <tr>
                <td style="background-color: white; "></td>

                <td style="text-align:right; float:none;" class=captiontiny>
  
                
                   <img  src="@imgFile" width="275"  />
                    <br />By  @_Photographer, on @_PhotoDateCreated<br />
      
          @*  @{ System.Web.Mvc.HtmlHelper.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" }); }*@
                  @h.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" })
                </td>
            
            <td style="background-color: white;"></td>
            </tr></table>


        </td>
           <td rowspan=4 style="background-color: white; width:0px"></td></tr>
         <tr>
             <td style="text-align:center;" class=caption><b>@_picCount.ToString().  @_PhotoCaption</b></td>
         </tr>
        <tr>
             <td style="text-align:left;" class=subcaption>@_PhotoComments</td>
         </tr>

        <tr style="height:10px"><td style="background-color: white;" colspan=3 class=caption></td></tr>

    </table>
    
}

 @PhotoImg.CreateImg(this.Html,"right", Model.Photo[0].PhotoID, Model.Photo[0].PhotoRecID, Model.Photo[0].PhotoFileType, Model.Photo[0].Photographer, Model.Photo[0].PhotoDateTaken, picCount, Model.Photo[0].PhotoCaption, Model.Photo[0].PhotoComments)

Thanks

Best Regards              


Monday, May 19, 2014 3:26 PM

Sounds like you need to add a using statement near the top of your PhotoImg.cshtml file: 

@using System.Web.Mvc.Html;

Monday, May 19, 2014 3:55 PM

I thought that was going to be the answer, but, now I get more errors.

Error 2 Instance argument: cannot convert from 'System.Web.WebPages.Html.HtmlHelper' to 'System.Web.Mvc.HtmlHelper' c:\MVCProjects\CWBFM\CWBFM\App_Code\PhotoImg.cshtml 28 17 CWBFM

Error 1 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string, string, System.Web.Routing.RouteValueDictionary, System.Collections.Generic.IDictionary<string,object>)' has some invalid arguments c:\MVCProjects\CWBFM\CWBFM\App_Code\PhotoImg.cshtml 28 17 CWBFM


Monday, May 19, 2014 4:30 PM

Try also adding a using for System.Web.Mvc


Monday, May 19, 2014 5:16 PM

Ok, I added

@using System;
@using System.Web;
@using System.Web.Mvc;

Still get

Error 1 'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) c:\MVCProjects\CWBFM\CWBFM\App_Code\PhotoImg.cshtml 30 19 CWBFM

When I try

   @{ System.Web.Mvc.HtmlHelper.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" });

and several variation on this. I get

Error 1 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink' c:\MVCProjects\CWBFM\CWBFM\App_Code\PhotoImg.cshtml 30 42 CWBFM

May have to rethink all this.


Monday, May 19, 2014 5:58 PM

Sorry I can't reproduce it. Can you make a dummy project with the same error and share it with me?


Monday, May 19, 2014 6:10 PM

Error      1              'System.Web.WebPages.Html.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.WebPages.Html.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)      

Thats a limitation see here, you might want to pass the HtmlHelper as parameter to your helper method and use it to call the built-in helper functions.


Monday, May 19, 2014 7:24 PM

Not sure if I can. I am about ready to pack it in for the night.

Did you notice this help is in App_Code, its not a HTMLHelper.

So:

App_Code

      PhotoImg.cshtml

    @using System;
    @using System.Web;
    @using System.Web.Mvc;

@helper CreateImg(string _picAlign, int _PhotoID, string _PhotoRecID, string _PhotoFileType, string _Photographer, string _PhotoDateCreated, int _picCount, string _PhotoCaption, string _PhotoComments)
{

     
             var imgFile = "/Images/Parks/" + _PhotoRecID + "." + _PhotoFileType;           // file name + file Type (jpeg, png, gif etc)
   
   
   <table style="float:@_picAlign; padding-bottom:2em; margin-left:0.2px;">

       <tr style="height:10px"><td style="background-color: white;" colspan=3 class=caption></td></tr>
       <tr><td rowspan=4 style="background-color: white; width:0px"></td>

        <td style="float: none," class=captiontiny>
       
    
            <table style="float: none;" border=0>
            <tr>
                <td style="background-color: white; "></td>

                <td style="text-align:right; float:none;" class=captiontiny>
  
                
                    <img  src="@imgFile" width="275"  />
                    <br />By  @_Photographer, on @_PhotoDateCreated<br />
      
          @*  @{ System.Web.Mvc.HtmlHelper.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" }); }*@

                </td>
            
            <td style="background-color: white;"></td>
            </tr></table>


        </td>
           <td rowspan=4 style="background-color: white; width:0px"></td></tr>
         <tr>
             <td style="text-align:center;" class=caption><b>@_picCount.ToString().  @_PhotoCaption</b></td>
         </tr>
        <tr>
             <td style="text-align:left;" class=subcaption>@_PhotoComments</td>
         </tr>

        <tr style="height:10px"><td style="background-color: white;" colspan=3 class=caption></td></tr>

    </table>
    
}

Stripping most of the view:

@model CWBFM.Models.MonumentDetails

@{
    int picCount = 1;
    int numberOfPics = Model.Photo.Count();
    string picAlign = "left";
    bool needComma = true;
    string savedName = "";
    string savedDate = "";
    string checkDate = "";
    string WasWere = " was ";

}



@section mainPage {

        @{ if (numberOfPics != 0)
           {
        @PhotoImg.CreateImg("right", Model.Photo[0].PhotoID, Model.Photo[0].PhotoRecID, Model.Photo[0].PhotoFileType, Model.Photo[0].Photographer, Model.Photo[0].PhotoDateTaken, picCount, Model.Photo[0].PhotoCaption, Model.Photo[0].PhotoComments);
              if (Model.Photo[0].PhotoAnonymous)
               {
                 savedName = "Anonymous";
               }
             else
               {
                 savedName = Model.Photo[0].PhotoCreatedBy;
               }

                savedDate = Model.Photo[0].PhotoDateCreated.ToString("MM/dd/yyyy");
                ViewBag.photoCredit = picCount.ToString(); 
                @PhotoImg.CreateCredit( picCount,  Model.Photo[0].PhotoCreatedBy,  Model.Photo[0].PhotoDateCreated,  Model.Photo[0].PhotoAnonymous,  savedName,  savedDate,  ViewBag.photoCredit,  needComma,  WasWere ) 
               
                
                picCount++;
           }}
  }

Controller

using System;
using System.Net;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text.RegularExpressions;
using CWBFM.Models;
using CWBFM.DAL;

namespace CWBFM.Controllers
{
    public class MonumentController : BaseController
    {
        private CWBFMContext db = new CWBFMContext();

         public ViewResult MonumentDetails(int id)
        {
            
            var monument = db.Monuments.Find(id);


            var photos = from s in db.Photo
                         where (s.PhotoStatus == "A") &&
                         (s.PhotoLinkRecID == monument.MonumentRecID)
                         select s;
           
            photos = photos.OrderBy(s => s.PhotoSequence);
            var vModel= new MonumentDetails();
            vModel.Monument = monument;
            vModel.Photo = photos.ToList();

            countView(monument.MonumentRecID, monument.MonumentCreatedBy); // count number of views
            ViewBag.battleName = getBattleName(monument.MonumentBattleRecID);
            return View(vModel);
       
        }

Tuesday, May 20, 2014 9:46 AM

That did it. Changed the action link to

    @h.ActionLink("enlarge", "LargePhoto", "Photo", new { id = @_PhotoID }, new { target = "_blank" }); 

(It was remarked out)

Thanks