Reputation: 4691
My MVC site works fine, it reads the data from EF and displays on page. I now have to add a "refine your search" section where customers can filter their results...
I have a horrible feeling my approach is wrong, if so, please do let me know!
My model looks like
using bconn.bll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace bconn.ui.Models
{
public class BoardToBoard
{
private bconnEntities _dataContext = new bconnEntities();
public IEnumerable<bll.BoardToBoard> BoardToBoardConnectors
{
set { }
get
{
return _dataContext.BoardToBoards.ToList();
}
}
public IEnumerable<bll.BoardToBoard> BoardToBoardSearch
{
get{}
}
}
}
And my View:
@model bconn.ui.Models.BoardToBoard
@{
ViewBag.Title = "Test";
}
<table>
@foreach (var item in Model.BoardToBoardConnectors) {
<tr>
<td>
@Html.DisplayTextFor(Model.BoardToBoardConnectors.Select(a=> a.Gender)) //FAULT
</td>
</tr>
}
</table>
The issue I have, is the DisplayTextFor expects a System.Linq.Expressions.Expression<System.Func<BoardToBoard,TResult>>)
The problem is, I'm not using the class (BoardToBoard), I'm only using a property of BoardToBoard (BoardToBoardConnector), as shown in the foreach statement.
How can I use the DispalyTextFor for a property of the class? I assume I've miss understood the requirements of MVC?
Upvotes: 0
Views: 76
Reputation: 13425
This is how I would go:
@foreach (var item in Model.BoardToBoardConnectors) {
<tr>
<td>
@Html.DisplayTextFor(m => item.Gender)
</td>
</tr>
}
You can use DisplayTextFor
for DataAnnotation formattings and sintax would be close to properties from Model
you used to (@Html.DisplayTextFor(m => m.Title)
).
Upvotes: 1
Reputation: 126
Do you need the DisplayTextFor? Can you just do something like this?
@foreach (var item in Model.BoardToBoardConnectors) {
<tr>
<td>
item.Gender
</td>
</tr>
}
or this:
@for(int i = 0; i < Model.BoardToBoardConnectors.Count; i++ )
{
<tr>
<td>
@Html.DisplayTextFor(m=>m.BoardToBoardConnectors[i].Gender)
</td>
</tr>
}
Upvotes: 1