简体   繁体   English

从ASP.NET MVC中的其他模型表访问字段

[英]Accessing a field from a differen't model's table in ASP.NET MVC

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? 在其他控制器/视图中工作时,是否可以通过该表访问字段?

Here is my code: 这是我的代码:

@{
    ViewBag.Title = "User Profile";
}

@model IEnumerable<FTv2.Models.Trade>

@{
    ViewBag.Title = "Active Trades";
    ViewBag.ImgUrl = ViewBag.Name + ".png";
}


<h2>@ViewBag.User's Profile:</h2>
<p>
    // This is where I would like to put the User info.
</p>
<h2>Active Trades:</h2>
<p>

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

@foreach (var item in Model)
{
    if (item.User == ViewBag.User)
    {
    <tr>
        <td>
            @{ViewBag.ImgUrl = @item.Name + ".png";}
        <a href="/Images/@ViewBag.ImgUrl"><img src="/Images/@ViewBag.ImgUrl" HEIGHT="66" WIDTH="50" ></a>
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price) TE
        </td>
        <td>
            <a href="/ActiveTrades/@item.User">@Html.DisplayFor(modelItem => item.User)</a>
        </td>
        @{if (ViewBag.User == User.Identity.Name){
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })
        </td>
        }}
    </tr>
    }
}

</table>

There are two options that I can think of: 我可以想到两种选择:

  1. Render an action method that returns the user details 呈现一个返回用户详细信息的操作方法

    @{ @ {

     Html.RenderAction("UserDetails", new { UserID = ViewBag.UserID}); 

    } }

  2. Create a new model that has the user details plus an IEnumerable of the trades and use this for your view. 创建一个具有用户详细信息以及IEnumerable交易的新模型,并将其用于您的视图。

I suppose that you wanna 我想你想

  • make page model that will contain the User Info and IEnumerable<...> . 制作将包含用户信息和IEnumerable<...>页面模型。
  • or pass User info into ViewBag 或将用户信息传递到ViewBag
  • or call @Html.RenderAction() 或致电@Html.RenderAction()

I think that you need to keep in mind the chance of reuse of the view model class. 我认为您需要牢记重用视图模型类的机会。 If you see no way to reuse the model then use ViewBag to pass the info into a View. 如果您看不到重用模型的方法,请使用ViewBag将信息传递到视图中。 In the Controller's Action set the property ViewBag.UserInfo = userInfo; 在控制器的操作中,设置属性ViewBag.UserInfo = userInfo;。 and in the view call: code @Html.Partial("_UserInfo", (UserInfo)ViewBag.UserInfo) code . 并在视图调用中: code @ Html.Partial(“ _ UserInfo”,(UserInfo)ViewBag.UserInfo) code But more practical way is to call @Html.Action(): 1. Make action UserController.UserInfo(int id). 但是更实用的方法是调用@ Html.Action():1.进行操作UserController.UserInfo(int id)。 2. Call from your view @Html.Action("UserInfo", "User", new {Id = ViewBag.UserId}); 2.从您的视图@ Html.Action(“ UserInfo”,“ User”,新的{Id = ViewBag.UserId})进行调用; 3. create partial view for the UserController.UserInfo action. 3.为UserController.UserInfo操作创建局部视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM