Software Developer
Software Developer

Reputation: 103

Filter data in other page in ASP.NET MVC

I have one table which is for contracts , And I have link beside each contract , when I click to the link they transfer to other page which is contract month but i want to display the data only for that contract which I click not all data.

Contact View :

@using (Html.BeginForm())
{

        @Html.ActionLink("Create", "Create")

            <table class="table table-bordered f" cellspacing="0">

                <thead>
                    <tr>
                    </tr>
                </thead>

                @foreach (var item in Model)
                {

                    <tbody>
                        <tr>


                            <td>
                                @Html.DisplayFor(modelItem => item.Contract_Num)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.Contract_Start)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.Contract_End)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.Status)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.typeOfRent.TypeOfRent_Name)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.AmountOfRent)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.Total)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.customers.Customer_Name)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.sections.Section_Name)
                            </td>


                            <td>
                                @Html.ActionLink("Contract Month", "Index", "ContractMonth")
                            </td>


                    </tbody>
                }
            </table>

contract month view :

   foreach (var item in Model)
                {

                        <tr>

                            <td>
                                @Html.DisplayFor(modelItem => item.contracts.Contract_Num)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.Monthe)
                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => item.contracts.AmountOfRent)
                            </td>

                            <td>
                                @Html.DisplayFor(modelItem => item.Receipt)
                            </td>

                            <td>
                                @{
                    var Balance = item.contracts.AmountOfRent - item.Receipt;
                                }

                                @Balance
                            </td>

Contract month controller:

  public ActionResult Index()
  {
   var ContractMonth =_context.ContractMonth.Include(s => s.contracts).ToList();

      return View(ContractMonth);

    }

Upvotes: 2

Views: 164

Answers (2)

er-sho
er-sho

Reputation: 9771

First of all change your Index action method inside ContractMonthController to get specific records with respect to ContractId

public ActionResult Index(int id)
{
    var contractMonth = _context.ContractMonth.Where(c => c.ContractsId == id).Include(s => s.contracts).ToList();

    return View(contractMonth);    
}

And then change your action to accept id as query string parameter,

<td>
    @Html.ActionLink("Contract Month", "Index", "ContractMonth", new { id = item.Contracts_Id }, null)
</td>

Upvotes: 3

Amir Molaei
Amir Molaei

Reputation: 3840

You can add the primary key (e.g. Id) of each contract to the url of each link as a query string. Then when getting the contract month data you can use the pk parameter to filter the data, for example (in the contract month controller):

  public ActionResult Index(int contractId)
  {
      var ContractMonth =_context.ContractMonth.Where(c => c.ContractId == contractId).Include(s => s.contracts).ToList();
      return View(ContractMonth);
    }

Upvotes: 3

Related Questions