I have a Major
model like this:
public int Id { get; set; }
public string MajorName{ get; set; }
public string Password { get; set; }
public System.DateTime RegisterDate { get; set; }
public string StudentId { get; set; }
public string State { get; set; }
public int FacultyId { get; set; }
public int MajorId { get; set; }
public HttpPostedFileBase ImgFile { get; set; }
So I have a method like this in my repository for the above model:
public Major FindMajorById(int id)
{
return _dbcontext.Majors.Find(id);
}
In my view I passed the id and I need the name of major:
<td>
@{
EducationRepositor.MajorRepository objmajor=new MajorRepository();
}
@Html.DisplayFor(objmajor.FindMajorById(modelItem => int.Parse(item.MajorId)).MajorName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Degree)
</td>
But I get error in my view in this line:
@Html.DisplayFor(objmajor.FindMajorById(modelItem => int.Parse(item.MajorId)).MajorName)
My error:
argument type lambda expression is not assignable to parameter type int
I suspect you've basically got your lambda expression and your method call the wrong way round. You may want:
@Html.DisplayFor(modelItem => objmajor.FindMajorById(int.Parse(modelItem.MajorId)).MajorName)
In other words, you're creating a lambda expression which takes a model item, parses its MajorId
property, calls FindMajorById
, and then uses the MajorName
property of the result. That lambda expression is used as the argument to DisplayFor
.
I'm not an MVC expert (by any means) so it's not clear how much benefit the lambda expression provides, however - whether you could actually just call the method directly.
See more on this question at Stackoverflow