---
title: "Access to a variable from a view in another view"  
description: "Access to a variable from a view in another view"  
author: "Anonymous User"  
published: 2013-12-23  
updated: 2013-12-23  
canonical: https://www.mindstick.com/forum/1817/access-to-a-variable-from-a-view-in-another-view  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# Access to a variable from a view in another view

I have the following [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) with applpication:

## Index.cshtml:

```
@model IEnumerable<ForestryManagement.Models.Forestry> @foreach (var forestry in Model)    {       
<li>@Html.ActionLink(forestry.Name, "Index12", new {tree
= forestry.ForestryID})    }
```

When you [click](https://www.mindstick.com/articles/12423/social-login-magento-2-one-click-to-register-social) on one Forestry (ActionLink), you will be redirected to the Index12, where the [trees](https://yourviews.mindstick.com/story/3817/high-pranic-trees-to-plant-in-your-garden-like-peepal) from these Forestry are listed

## Index12.cshtml:

```
@model IEnumerable<ForestryManagement.Models.Tree>@foreach (var item in Model) {    <tr>        <td>            @Html.DisplayFor(modelItem => item.treeName)        </td>        </tr>@Html.ActionLink("Create new Tree", "Create", new { id = **??????**})
```

[Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now), when i will create another tree in this forestry, i need the ID from the forestry on my Index12.cshtml [page](https://www.mindstick.com/articles/13031/why-to-make-a-wikipedia-page) for the ActioLink

## Replies

### Reply by Pravesh Singh

Hi Ankit,

Create a view model for tree listings, like so:

```
public class TreeListingViewModel{    public int
ForestryId { get; set; }    public
IEnumerable<TreeViewModel> Trees { get; set; }}
```

Note: Prefer creating a TreeViewModel instead of just using your Tree data entity to reduce the dependency between your view and your data model (and also increase security as people can quite easily inject properties into your database if you expose your data entities directly to your view).

## Pass that to your view instead:

@model TreeListingViewModel

@Html.ActionLink("Create new Tree", "Create", new { id = Model.ForestryId })


---

Original Source: https://www.mindstick.com/forum/1817/access-to-a-variable-from-a-view-in-another-view

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
