Reputation: 53
I cant assign method group to anonymous type property, here is the code snippet.
@model MineKlonerApp.Models.Klone[]
@{
ViewBag.Title = "Kloner";
}
<h2>@ViewBag.Title</h2>
<div class="row">
@foreach (var klone in Model)
{
<div class="col-md-3">
<h4>@Html.ActionLink(klone.displayText, "Detail", new {id = klone.Id})</h4>
<img src="~/Images/peter-griffin-one.png"
alt="@klone.displayText"
class="img-responsive" />
</div>
}
</div>
The "new {id = klone.Id})" is where I have an issue. The klone.Id is being collected from another file named, kloneRepository.cs
Here is example of that code.
private static Klone[] _klonePerson = new Klone[]
{
new Klone()
{
Id = 1,
Navn = "Peter Griffin",
Alder = 42,
Beskrivelse = "feit og dum",
}
I get the error, that I cant assign it to anonymous, is there maybe another way I can do this or does anyone know how to fix this and explain it to me, I would be grateful.
public class Klone
{
internal int Id;
public string Navn { get; set; }
public int Høyde { get; set; }
public string Kjønn { get; set; }
public string displayText
{
get
{
return Navn;
}
}
public string imageFile => Navn.Replace(" ", "-").ToLower() + "-" + ".jpg";
public int Alder { get; internal set; }
public string Beskrivelse { get; internal set; }
}
}
this is what the klone.cs class looks like
Upvotes: 0
Views: 7413
Reputation: 376
For anyone who googled the error message and ended up here, and the other answers don't apply as in my case, I was using a linq query with the .FirstOrDefault function and I neglected to add the parenthesis's, once I changed it to .FirstOrDefault(), the error went away.
Upvotes: 0
Reputation: 118937
I thought that I'd add a little extra info to the answer by @Shyju. While his answer is technically correct (i.e. "make it a public property") the part he focused on is not. It's actually the fact that it's an internal
field that is the problem so all you need to do is make it public
. Of course, using a real property is still preferred. To summarise:
internal int Id; //Will NOT work
public int Id; //Will work
internal int Id { get; set; } //Will NOT work
public int Id { get; set; } //Will work
Upvotes: 1
Reputation: 218722
Because Id
is a field of Klone
class. You need to convert that to a public property.
public class Klone
{
public int Id { set; get; }
// Your other properties goes here
}
The overload of Html.ActionLink
helper method you are using is expecting an object which is made of parameters of the route. You should be using the properties of an object here to build the parameters.
Upvotes: 2