Alexander Groza February 2016

How to add a link to related data MVC

I have customer model

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; }
}

and systemValue model

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; }
}

in wich way i could show a link in CustomerView(CustomersController) foreach customer that redirect to the SystemValuesView(SystemValuesController) with related to this customer SystemValues?

i found out one way - redirect to this controller's action with parametr.

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

But i'm sure there must be smarter way.

Answers


SwedishProgrammer February 2016

 @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>
                }

I hope I understood you correctly. This code should go in the view. The view should be strongly typed to a model.

Code is if you want a button that redirects to the index view of the SystemValues controller, with the CustomerId as input. You should change "yourcustomerlist" to the list containing the customer information. If it's not part of a table, remove the table-related tags (<td> and <tr>).

Post Status

Asked in February 2016
Viewed 3,114 times
Voted 10
Answered 1 times

Search




Leave an answer