gpt4 book ai didi

c# - DATEDIFF在Asp.net MVC中怎么写

转载 作者:太空宇宙 更新时间:2023-11-03 22:28:51 25 4
gpt4 key购买 nike

我正在创建一个汽车零售系统。我需要显示如果我输入正确的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>

返回 Controller

 [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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com