1

Is there a way to use a condition in the ASP MVC3 @Html.DisplayFor() control? Below is the syntax I attempted. While it seems the partner.TradingPartners is able to use .Where<>, according to Visual Studio I am doing it wrong!

Here is the syntax I use

        @foreach (var item in Model.Agent.SymNumberToAgentId)
        {
            var gogo = ViewBag.Periscope + item.AgentId.Trim();
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.DisplayAgentId)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.CompanyCode)
                </td>
                <td>
                    @foreach (var partner in Model.AgentIdDetails)
                    {
                        @Html.DisplayFor(modelItem => partner.TradingPartners.Where(item => item.AgentId == partner.AgentId))
                    }
                </td>
                <td>
                    @Html.ActionLink("Remove", "Delete", "SymetraNumberToAgentId", new { id = item.SymetraNumber, aid=item.AgentId}, null)
                </td>
                <td>
                    <a href=@gogo target="_blank">View in Periscope</a>
                </td>
            </tr>
        }

and here is the error message

CS1928: 'System.Collections.Generic.IList<string>' does not contain a definition for 'Where' and the best extension method overload 'System.Linq.Enumerable.Where<TSource>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)' has some invalid arguments

1
  • 7
    Much cleaner to filter this list in the controller Commented Feb 11, 2013 at 21:52

1 Answer 1

2

Edit: After the question was updated (code inside foreach):

      @foreach (var partner in Model.AgentIdDetails)
                    {
                        @Html.DisplayFor(modelItem => partner.TradingPartners.Where(x => x.AgentId == item.AgentId).XYZProperty)
                    }

You need to use Proper syntax in the where clause. Also, you should specify the Property for the filtered Trading Partner.

<td>
    @foreach (var partner in Model.AgentIdDetails)
    {
        @Html.DisplayFor(modelItem => partner.TradingPartners.Where(item => item.AgentId == partner.AgentId).XYZProperty)
    }
</td>
Sign up to request clarification or add additional context in comments.

5 Comments

Deleted my answer, you beat me to it. But I still think it's valid to consider what @Forty-Two said... this is probably better done in the controller.
True. The best practice is to do it in the controller. +1 to @Forty-Two
Sorry, I should have been clearer in my description of the code. This is taking place inside a foreach loop
Ugh.. the edited answer resulted in the following error message: 'string' does not contain a definition for 'AgentId' and no extension method 'AgentId' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
I believe the issue is there is no AgentId property in the TradingPartners list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.