[英]DATEDIFF how to write in Asp.net MVC
我正在创建一个汽车零售系统。 我需要显示如果我输入正确的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>
返回控制器
[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();
文件。
在 C# 中,您不需要像在 SQL 中那样编写 Datediff() 函数。 您只需减去所需的日期:
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
});
请尝试以下代码:
var carn = (from s in db.rentails where s.carno == carno select s)
.Select(x => new
{
DateDiff = (x.edate - x.sdate).Days
});
注意:确保 eDate 的类型,sDate 是 DateTime。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.