-1

Intellisense is throwing this error when I try to implement an Html.ActionLink in my Index View:

"System.Web.Mvc.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)' has some invalid arguments"

I have verified I have the correct references and checked my web.config file within the views folder and have what appears to be the correct namespaces, below is the namespaces within that web.config

<namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
  </namespaces>

Here is my Index.cshtml

@model IEnumerable<Madison_TestApp.Models.Notes>

@using Madison_TestApp.Models;

@{
    ViewBag.Title = "Notes List";
}

<h2>Notes List</h2>
    <ul>
        @foreach(Notes note in @Model)
        {
           <li>
               ***@Html.ActionLink(note.ProductID, "Details", new { id = note.ID })***
           </li> 
        }
    </ul>

Here is my Controller Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Madison_TestApp.Models;
using System.Web.Mvc.Html;

namespace Madison_TestApp.Controllers
{
    public class NotesController : Controller
    {
        public ActionResult Index()
        {
            NotesContext notesContext = new NotesContext();
            List<Notes> notes = notesContext.Notes.ToList();

            return View(notes);
        }

        public ActionResult Details(int id)
        {

            NotesContext notesContext = new NotesContext();
            Notes note = notesContext.Notes.Single(prodnote => prodnote.ID == id);

            return View(note);
        }
    }

}

And Here is my model code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc.Html;

namespace Madison_TestApp.Models
{
    [Table("ProductNotes")]
    public class Notes
    {
        public int ID { get; set; }
        public int ProductID { get; set; }
        public string NoteText { get; set; }
        public DateTime CreateDate { get; set; }
        public bool Archived { get; set; }
    }
}
Jrobbie
  • 9
  • 6
  • `@Html.ActionLink(note.ProductID, "Details", new { id = note.ID })` should be changed so that `note.ProductID` is replaced by a string. `ActionLink` takes in a string as its first argument and `note.ProductID` is an int. Also in your foreach loop remove `@` from infront of `Model` – urnotsam Sep 30 '14 at 18:35

2 Answers2

0

You can change your for each loop to a for loop and call your ActionLink like this

 @for (var i = 0; i < Model.Count(); i++) 
 {
     @Html.ActionLink("Link Text", "ActionMethod", "Controller", new { id = Model[i].Productid }, null)
 }

Edit: if you want to use a for each loop this overload should work

@Html.ActionLink("Action Link", "ActionURL", "Hello", new { id = @Notes.ProductId }, null)
CSharper
  • 5,420
  • 6
  • 28
  • 54
  • 1
    And if you want to use the ProductId as the link text, just replace "Link Text" to Model[i].ProductID.ToString(). – JB06 Sep 30 '14 at 18:46
  • This answer will probably help you out as well http://stackoverflow.com/questions/10298823/html-actionlink-one-works-and-one-does-not – CSharper Sep 30 '14 at 18:52
  • Well, **of course you CAN use ActionLink in foreach**. The PO's problem is just as @Josh mentioned, the type must be `string` for the link text, in this case `note.ProductID` is a int, and must be cast into string. – tweray Sep 30 '14 at 18:56
  • @tweray my mistake, I know sometimes for each can be problematic – CSharper Sep 30 '14 at 19:00
0

For you question, you just need to change the part of your view to:

    <ul>
        @foreach(Notes note in Model)
        {
           <li>
               @Html.ActionLink(note.ProductID.ToString(), "Details", new { id = note.ID })
           </li> 
        }
    </ul>

The 2 things I changed are:

  1. Removed the @ of your @Model in foreach. You don't need the @ sign inside c# code
  2. Changed note.ProductID to note.ProductID.ToString(). Keep in mind that your c# code running inside view is still c#, you have to pass the right type into a method, in this case the Link Text parameter for Html.ActionLink must be a string, then you have to cast your note.ProductID(which is an int) to string

Besides, welcome to stackoverflow! Just a kindly reminder, that some of us (including myself) will sometimes provide our suggestion in comment instead of write an answer if it can be finished in 1 or 2 sentences.

I must admit that's not a good habit, but it will always be helpful when you are seeking answers to also read some comments that people left here. In this case @urnotsam already have a solution for you in comment, and I am just moving his suggestion into answer so you can realize it.

tweray
  • 1,002
  • 11
  • 18