在View中有一个选择链接。单击时,显示有关所选项目的更多信息

时间:2016-04-20 14:05:23

标签: c# asp.net .net asp.net-mvc entity-framework

我想添加允许用户选择资产的功能,并在选择后显示有关所选资产的其余信息。

这是我的索引视图:

@model IEnumerable<AssetManagementSystem.Models.Asset>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.AssetMake.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AssetType.Type)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.CPU.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Department.DepartmentName)
        </th>

        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.AssetMake.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.AssetType.Type)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CPU.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Department.DepartmentName)
        </td>

        <td>
            @Html.ActionLink("Select", "Index", new { id = item.AssetID }) |
            @Html.ActionLink("Edit", "Edit", new { id=item.AssetID }) |
            @Html.ActionLink("Details", "Details", new { id=item.AssetID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.AssetID })
        </td>
    </tr>
}

</table>

基本上,我希望在用户点击“#34;选择&#34;”时显示有关资产的其他信息。

这是我的AssetModel:

 public class Asset
 {
    [Key]
    public int AssetID { get; set; }
    public int AssetMakeID { get; set; }
    public int AssetTypeID { get; set; }
    public string User { get; set; }
    public int DepartmentID { get; set; }
    public string AssetModel { get; set; }
    public string AssetSerialNo { get; set; }
    public string PurchaseOrderNo { get; set; }
    public string Supplier { get; set; }
    public string WarrantyInfo { get; set; }
    public DateTime DatePurchased { get; set; }
    public string ComputerName { get; set; }
    public int CPUID { get; set; }
    public string RAM { get; set; }
    public string HardDrive { get; set; }
    public int OperatingSystemID { get; set; }
    public string OperatingSystemProductKey { get; set; }

    public virtual AssetType AssetType { get; set; }
    public virtual Department Department { get; set; }
    public virtual CPU CPU { get; set; }
    public virtual OperatingSyst OperatingSyst { get; set; }
    public virtual AssetMake AssetMake { get; set; }

以下是我的资产管理器中的索引方法:

public ActionResult Index()
{
    var asset = db.Asset
        .Include(a => a.AssetMake)
        .Include(a => a.AssetType)
        .Include(a => a.CPU)
        .Include(a => a.Department)
        .Include(a => a.OperatingSyst);
    return View(asset.ToList());
}

如何添加此功能以允许用户选择资产,然后显示资产信息?

1 个答案:

答案 0 :(得分:0)

你已经拥有它。

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

然后在您的详细信息页面上放置Html帮助程序以获取您想要显示的其他信息。我正在使用MVC 6但是同样的东西可以应用于你正在使用的任何版本。目标=&#34; _blank&#34;将只在另一个标签中打开,以便他们可以轻松返回。

            <a asp-action="Edit" asp-route-id="@item.AssetId">Edit</a> |
            <a asp-action="Details" asp-route-id="@item.AssetId" target="_blank">Details</a> |
            <a asp-action="Delete" asp-route-id="@item.AssetId">Delete</a>