records display not match with the records in database

Last Reply 5 days ago By dharmendr

Posted 5 days ago

Hi, I have a system that allow user to choose car model based on the order number. User can choose the order using checkbox and after choose, user will click button send email and the email body should contain order number and car model.

 

The issue is with the email body. For example if the order number have 5 types of model, the table should display 5 records, but it display 15 records instead of 5 records. i think it might be the foreach loop but not sure how to do it. 

 

Model

public class CarModel
    {
        public int id { get; set; }
        public int order_num { get; set; }
        public Nullable<System.DateTime> order_date { get; set; }
        public string uniNum { get; set; }
        public string color { get; set; }
	public string model{ get; set; }
}

 

Action

 

public ActionResult Index()
        {
            var updateList = (from od in db.car_details
                              join a in db.agreement
                              on od.order_num equals a.order_no
                              join b in db.car_list
                              on od.uniNum equals b.uniNum                           
                              select new CarModel
                              {
                                 
                                  id = od.id,
                                  uniNum = od.uniNum,
                                  order_num = od.order_num,
                                  color = od.color,                              
                                  order_date = od.order_date,                             
                                  email_status = (bool)a.email_status,
				  model = b.model


                              }
                  ).Take(1000).ToList();

            if (updateList.Count() != 0)
            {
                ViewBag.updateList = updateList;
            }
            else if (updateList.Count() == 0)
            {
                ViewBag.updateList = null;
                ViewBag.message = "No records found!";
            }

            return View();
        }



 [HttpPost]
        public ActionResult Index(int[] chkBox)
        {
            var EmailBody = "<p>Hi Team,</p>" +
                            "Please proceed with below order(s) . Thanks.  <br/><br/>";
            EmailBody += "<table border = 1>";
            EmailBody += "<tr style='background - color: #4CAF50; border =1;'>";
            EmailBody += "<th>Order Number</th>";
            EmailBody += "<th>Car Model</th>";
            EmailBody += "</tr>";

            foreach (var item in chkBox)
            {
                var carOrder = (from od in db.car_details
                                     join a in db.agreement
                                     on od.order_num equals a.order_no
                                     join b in db.car_list
                                     on od.uniNum equals b.uniNum
                                     where
                                    od.id == item
                                     select new CarModel
                                     {
                                         order_num = od.order_num,
                                         model= a.model,
                                        
                                     }).ToList();

                foreach (var value in carOrder)
                {
                    EmailBody += "<tr>";
                    EmailBody += "<td>" + value.order_num + "</td>";
                    EmailBody += "<td>" + value.model + "</td>";
                 
                    EmailBody += "</tr>";

                    var updateEmail= db.agreement.Where(x => x.uniNum == value.uniNum).FirstOrDefault();
                    updateEmail.email_status = true;
                    
                }
                                
            }
            db.SaveChanges();



            EmailBody += "</table>";
           

            string EmailFrom = "automated@automotive.com";

            string subject = "Order process";

            EmailService(EmailFrom, subject, EmailBody);


            return RedirectToAction("Index", "Home");

        }

 

View

 

@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
    <input type="submit" value="Send Email" class="btn btn-success" align="right" />
    <br/><br/>
    <div class="table" id="table_id">
        <table class="table table-striped table-bordered table-hover table-sm display" id="dataTable" style="width:100%" cellspacing="0">
            <thead class="thead-dark">
                <tr>
                    <th></th>
                    <th class="text-center">Order Number</th>
                    <th class="text-center">Car Model</th>
                    <th class="text-center">Color</th>
                    <th class="text-center">Order Date</th>
                </tr>
            </thead>
            <tbody>
                @if (ViewBag.updateList != null)
                {
                    foreach (var item in (List<CarSample.Models.CarModel>)ViewBag.updateList)
                    {
                        <tr>
                            <td>
                                @if (item.email_status == false)
                                {
                                    <input type="checkbox" name="chkBox" value="@item.id" />


                                }
                                else if (item.email_status == true)
                                {
                                    <a href="" class="btn btn-primary" disabled>Email Sent</a>
                                }

                            </td>
                            <td>@item.order_num</td>
                            <td>@item.model</td>
                            <td>@item.color</td>
                            <td>@item.order_date</td>      
                        </tr>

                    }
                }
                else
                {

                }
            </tbody>

        </table>
    </div>

}

 

You are viewing reply posted by: dharmendr 5 days ago.
Posted 5 days ago
Steve says:
var carOrder = (from od in db.car_details
                                     join a in db.agreement
                                     on od.order_num equals a.order_no
                                     join b in db.car_list
                                     on od.uniNum equals b.uniNum
                                     where
                                    od.id == item
                                     select new CarModel
                                     {
                                         order_num = od.order_num,
                                         model= a.model,
                                         
                                     }).ToList();

Please verify the query. Does it returns 15 records based on checkbox value?