mardi 5 mai 2015

Duplicated data in MVC linq query

I have a question regarding linq query. My controller code looks like this:

public ActionResult Show_Trans(int id = 0)
    { 
        //this checks if the id exists in the database
        var check = db.Student_Trans.Where(s=>s.student_id == id).FirstOrDefault();
        // if not, show a javascript alert
        if(check == null){
            return Content("<script type='text/javascript'>alert('No transaction so far.');</script>");
        }

        // this return the data that equals to the id
        return PartialView(db.Payments_vw.Where(s=>s.student_id == id).ToList());

    }

In my database view (Payments_vw), for example, the student with student id of 2 has 3 transactions namely: Miscellaneous, Parents Share, Uniform.

The question is: when I tried to view it in my Views, it returns the exact number of rows but the datas are repeated or should I say, all are in Miscellaneous transaction but the Parents Share and Uniform are not shown. Why? Any help is greatly appreciated. Thanks.

Here is my view:

@model IEnumerable<Billing_App.Models.Payments_vw>

<h3><b>List of Transaction Logs</b></h3>
<table class="table">
<tr>
    <th>Student Name</th>
    <th>Transaction Name</th>
    <th>Transaction Amount</th>
    <th>Amount Paid</th>
    <th>Deadline</th>
    <th>Remaining Balance</th>
</tr>
@foreach (var item in Model)
{ 
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.student_lastname)&nbsp;, @Html.DisplayFor(modelItem => item.student_firstname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.trans_name)
        </td>
        <td>
            P &nbsp; @Html.DisplayFor(modelItem => item.trans_amount)
        </td>
        <td>
            P &nbsp; @Html.DisplayFor(modelItem => item.payment)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.trans_deadline)
        </td>
    </tr>

}

I think there's something wrong with my linq query.

Aucun commentaire:

Enregistrer un commentaire