1

I have a list of Topic's. Each Topic have a list of Message. When i click on "Details" I want to open a new View that display a list of the Message's from the Topic.

I expect that I need to modify this line: (this is what I have so far)

@Html.ActionLink("Details", "Details", new { id=item.Id }, null)

The Message's are accessed by item.Message.

And the first line in the Detail View

@model IEnumerable<Models.TopicMessage>


This is my Topic View:

@model IEnumerable<Models.Topic>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.Id }, null)
        </td>
    </tr>
}

</table>
4
  • I can't understand. You are creating links for each topic in that line. Do you want to display a partial view for Message list? Commented Sep 20, 2013 at 21:03
  • @UfukHacıoğulları I want to open a new View with all the Message this Topic contains. Commented Sep 20, 2013 at 21:04
  • Do you have a partial view for that? Commented Sep 20, 2013 at 21:07
  • I have only know MVC 4 for two days. I know partial Views is for views you can reuse in other views. But I thought here the first time I should just get it to work in a normal View before advange to Partial View, but that's maybe a bad strategy? Commented Sep 20, 2013 at 21:16

2 Answers 2

1

There are a number of ways to do that. You can use the Action method to execute the controller action directly:

<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.Action("Details", "Details", new { id=item.Id })
    </td>
</tr>

Or you can bypass the controller and render the view directly using this overload of DisplayFor:

<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Messages, "MessagesDetail")
    </td>
</tr>

Or if you define the template as the default DisplayTemplate, you can just do this:

<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Messages)
    </td>
</tr>
Sign up to request clarification or add additional context in comments.

8 Comments

Okay I will give it a try. It's a good idea to make the View strongly typed right? It should be like this right? @model IEnumerable<Models.TopicMessage>
@radbyx Yes, that's definitely preferred.
What about this instead @model IEnumerable<Models.Topic>? Im confused. Should I bring the Topic, that i could get the Messages from, or do I bring a list of Message?
@radbyx For a detail view of a single Topic entity, I'd generally recommend @model Model.Topic. This way you can enumerate the TopicMessages in that model or inspect other properties as well. If, however, this is just a list of messages (not exactuly a full detail view of the parent Topic), @model IEnumerable<Models.Topic> would be better, IMO. Of course, you might have a detail view which contains the list of messages, in which case you might have actually two views, but that's sort of up to your personal preferences.
@radbyx Sorry, the fourth parameter is for providing html attributes for the <a> tag. This isn't used in @Html.Action. I've updated my answer.
|
0

I fixed this by making a somethingViewModel that contained both the liste and the Id. That way i could contain the two information from two difference places in one new object I had access to in the View.

Comments

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.