简体   繁体   中英

DATEDIFF how to write in Asp.net MVC

I am creating a car retail system. I need to the display if I enter the correct carid relevant car information will be displayed in the below text boxes. I have attached the screen shot image below. I need to calculate day difference between start date and end date for calculation of how many days between because retail fee needs to be calculated for extra days of the retail system. In C# i wrote like this select car_id,cust_id,due,DATEDIFF(GETDATE(),due) as elap from rental where car_id = ? I don't know how to write in Asp.net MVC.

Code I tried:

form design

<div class="row">
    <div class="col-sm-8">

        @using (Html.BeginForm("Save", "return", FormMethod.Post, new { id = "popupForm" }))
        {
            <div>
                <h4> Registation</h4>
            </div>

              <div class="card-action">

                <label class="form-label">Car ID</label>
                  <input type="text" id="carno" name="carno" class="form-control" placeholder="carno" required />
            </div>



            <div class="card-action">

                <label class="form-label">Customer ID</label>

                <input type="text" id="custid" name="custid" class="form-control" placeholder="Customer ID" required />

            </div>



            <div class="card-action">

                <label class="form-label">Date</label>

                <input type="text" id="date" name="date" class="form-control" placeholder="Date" required />

            </div>


             <div class="card-action">

                <label class="form-label">Days Elapsed</label>

                 <input type="text" id="elap" name="elap" class="form-control" placeholder="Date" required />

            </div>


             <div class="card-action">

                <label class="form-label">Fine</label>

                 <input type="text" id="fine" name="fine" class="form-control" placeholder="Fine" required />

            </div>


            <div class="card">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>

        }
    </div>

</div>

jQuery seach the data using jquery

   <script>
        getProductcode();
        function getProductcode() {
            $("#carno").empty();
            $("#carno").keyup(function (e)
            {
                var q = $("#carno").val();
                $.ajax({
                    type: "POST",
                    url: '/return/Getid?carno=' + $("#carno").val(),
                    dataType: "JSON",
                    success: function (data)
                    {
                        console.log(data);
                          $('#custid').val(data.custid);
                          $('#date').val(data.sdate);
                           $('#elap').val(data.elap);

                    },
                    error: function (xhr, status, error)
                    {
                        //  alert("The barcode entered is not correct");
                    }
                });
                return true;
            });
        }

    </script>

returnController

 [HttpPost]
        public ActionResult Getid(String carno)
        {
            carrentEntities1 db = new carrentEntities1();
            var carn = (from s in db.rentails where s.carno == carno select s.custid).ToList();
            return Json(carn, JsonRequestBehavior.AllowGet);
        }

I don't know how to write the Datediff to calculate days. In this no result displayed. I tested custid , it didn't display.

Database fields

id  carno   custid  fee   sdate      edate
1   1        1     1200  2019-12-09  2019-12-19
2   1        1     20000 2019-12-01  2019-12-31
3   A0001    1     3434  2019-12-09  2019-12-27

You can use SqlFunctions.DateDiff like this :

   var carn = (from s in db.rentails where s.carno == carno 
       select new {   
       StartDate = s.sdate,
       EndDate = s.edate,
       CarNo = s.carno,
       Fee = s.fee,
       ElapsedDays = SqlFunctions.DateDiff("day",DateTime.Now,s.sdate)
    }).ToArray();

Document .

In C# you don't need to write Datediff() function like in SQL. You just can minus needed dates:

var carn = (from s in db.rentails where s.carno == carno select s).
.ToList()
.Select(x => new 
{
DateDiff = (x.edate - x.sdate).Days,
//rest props
});

Pls give it a try the following code:

var carn = (from s in db.rentails where s.carno == carno select s)
.Select(x => new 
{
  DateDiff = (x.edate - x.sdate).Days
});

Note : Make sure type of eDate, sDate is DateTime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM