Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a field from a differen't model's table in ASP.NET MVC
    primarykey
    data
    text
    <p>I have a page that displays a filtered table based on the user that is passed to the page. I also want to display the user's info a the top of the page, but this comes from a different table (from the Account model). Is there a way I can access fields from this table while working in my other controller/views?</p> <p>Here is my code:</p> <pre><code>@{ ViewBag.Title = "User Profile"; } @model IEnumerable&lt;FTv2.Models.Trade&gt; @{ ViewBag.Title = "Active Trades"; ViewBag.ImgUrl = ViewBag.Name + ".png"; } &lt;h2&gt;@ViewBag.User's Profile:&lt;/h2&gt; &lt;p&gt; // This is where I would like to put the User info. &lt;/p&gt; &lt;h2&gt;Active Trades:&lt;/h2&gt; &lt;p&gt; &lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt; &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Name) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.Price) &lt;/th&gt; &lt;th&gt; @Html.DisplayNameFor(model =&gt; model.User) &lt;/th&gt; &lt;th&gt;&lt;/th&gt; &lt;/tr&gt; @foreach (var item in Model) { if (item.User == ViewBag.User) { &lt;tr&gt; &lt;td&gt; @{ViewBag.ImgUrl = @item.Name + ".png";} &lt;a href="/Images/@ViewBag.ImgUrl"&gt;&lt;img src="/Images/@ViewBag.ImgUrl" HEIGHT="66" WIDTH="50" &gt;&lt;/a&gt; &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Name) &lt;/td&gt; &lt;td&gt; @Html.DisplayFor(modelItem =&gt; item.Price) TE &lt;/td&gt; &lt;td&gt; &lt;a href="/ActiveTrades/@item.User"&gt;@Html.DisplayFor(modelItem =&gt; item.User)&lt;/a&gt; &lt;/td&gt; @{if (ViewBag.User == User.Identity.Name){ &lt;td&gt; @Html.ActionLink("Edit", "Edit", new { id = item.ID }) | @Html.ActionLink("Details", "Details", new { id = item.ID }) | @Html.ActionLink("Delete", "Delete", new { id = item.ID }) &lt;/td&gt; }} &lt;/tr&gt; } } &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload