locked
Duplicate Entries in Views RRS feed

  • Question

  • User-2042784146 posted

    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>

    }
    </table>


    I think there's something wrong with my linq query.
    I really need your help...

    Tuesday, May 5, 2015 10:42 PM

All replies

  • User-271186128 posted

    Hi jynevily,

    From your code and description, if you want to exclude duplicates, you could try to use Distinct() Mehod. 

    For more details about Distinct() method, please see: https://msdn.microsoft.com/en-us/library/vstudio/bb348436(v=vs.100).aspx


    all are in Miscellaneous transaction but the Parents Share and Uniform are not shown.

    As for this issue, I suggest you check the Payments_vw View.

    Best Regards,
    Dillion

    Thursday, May 7, 2015 5:34 AM
  • User-2042784146 posted

    Hi Zhi Lv, thanks for the reply.

       

        "all are in Miscellaneous transaction but the Parents Share and Uniform are not shown."

    I already checked the view in here and it's the right data that should be shown. I only have a problem why duplicates are there when displaying.

    Tuesday, May 12, 2015 3:46 AM