如何向相关数据 MVC 添加 link

How to add a link to related data MVC

我有客户模型

public class Customer
{
    public Customer()
    {
        this.SystemValues = new HashSet<SystemValue>();
    }

    public string Name { get; set; }
    public Nullable<System.Guid> GUID { get; set; }
    public int Id { get; set; }

    public virtual ICollection<SystemValue> SystemValues { get; set; }
}

和系统价值模型

public class SystemValue
{
    public int CustomerId { get; set; }
    public int SystemValueId { get; set; }
    public Nullable<int> SystemValueCategoryId { get; set; }
    public string SystemValueType { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string TextValue { get; set; }
    public Nullable<int> IntValue { get; set; }
    public Nullable<double> FloatValue { get; set; }
    public byte[] BlobValue { get; set; }
    public Nullable<System.DateTime> DateTimeValue { get; set; }
    public Nullable<bool> BooleanValue { get; set; }
    public Nullable<int> LookupValueId { get; set; }
    public Nullable<int> LookupValueGroupId { get; set; }
    public Nullable<bool> IsReadonly { get; set; }
    public bool IsHidden { get; set; }
    public int Id { get; set; }

    public virtual Customer Customers { get; set; }
}

我可以用哪种方式在 CustomerView(CustomersController) 中为每个重定向到与该客户 SystemValues 相关的 SystemValuesView(SystemValuesController) 的客户显示 link?

我找到了一种方法 - 使用参数重定向到此控制器的操作。

public ActionResult ViewSystemValues(int? id)
    {
        return RedirectToAction("Index", "SystemValues", new {id});
    }

但我相信一定有更聪明的方法。

 @for(int i = 0; i < Model.yourcustomerlist.Count(); i++)
                {
        <tr>
            <td>
                <a class="btn btn-primary" href="@Url.Action("Index", "SystemValues", new { id = Model.yourcustomerlist[i].CustomerId })">
   <b>Go to system values</b>
                </a>
            </td>
        </tr>
                }

希望我理解正确。 这段代码应该在视图中。视图应该强类型化到模型。

代码是如果你想要一个重定向到 SystemValues 控制器的索引视图的按钮,并将 CustomerId 作为输入。您应该将 "yourcustomerlist" 更改为包含客户信息的列表。如果它不是 table 的一部分,请删除与 table 相关的标签(<td><tr>)。