我正在创建一个汽车零售系统。如果输入正确的carid,则需要显示,相关的汽车信息将显示在以下文本框中。我已附上下面的屏幕截图。我需要计算开始日期和结束日期之间的天差,以计算之间的天数,因为需要为零售系统的额外天数计算零售费用。在C#中,我这样写select car_id,cust_id,due,DATEDIFF(GETDATE(),due) as elap from rental where car_id = ?
我不知道如何用Asp.net MVC编写。

我试过的代码:

表格设计

<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使用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);
        }


我不知道如何写Datediff来计算天数。在此没有结果显示。我测试了custid,它没有显示。

数据库字段

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

最佳答案

您可以这样使用SqlFunctions.DateDiff

   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

关于c# - DATEDIFF如何在Asp.net MVC中编写,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59248498/

10-13 03:38