很抱歉我的问题,但我是 MVC 的新手。
这是我的情况。
在我看来,我有一个模型(@model DAEF.Models.M_Generic)
我想比较 2 个字段。我的问题是,如何使用 javascript
为了做到这一点?
在我的 example 代码下方有 2 个日期。
@model DAEF.Models.M_Generic
<script type="text/javascript">
function CompareDate() {
var dSart = "<%=model.Dat_Start%>";
var dEnd = "<%=model.Dat_End%>";
if (dEnd > dSart) {
alert("Date One is greather then Date Two.");
}
}
CompareDate()
</script>
@using (Html.BeginForm("Ask_History", "Corr_Exit"))
{
@Html.AntiForgeryToken()
<div class="row">
@Html.LabelFor(model => model.Dat_Start, new { @class = "control-label col-sm-2" })
<div class="col-sm-3">
@Html.TextBoxFor(model => model.Dat_Start, new { @class = "DateTimePicker form-control" })
@Html.ValidationMessageFor(model => model.Dat_Start, "", new { @class = "text-danger" })
</div>
@Html.LabelFor(model => model.Dat_End, new { @class = "control-label col-sm-2" })
<div class="col-sm-3">
@Html.TextBoxFor(model => model.Dat_End, new { @class = "DateTimePicker form-control" })
@Html.ValidationMessageFor(model => model.Dat_End, "", new { @class = "text-danger" })
</div>
</div>
}
最佳答案
您可以使用 ToShortDateString()
并将该字符串转换为 javascript Date
,例如:
<script type="text/javascript">
function CompareDate() {
var dSart =new Date("@model.Dat_Start.ToShortDateString()");
var dEnd = new Date("@model.Dat_End.ToShortDateString()");
if (dEnd > dSart) {
alert("Date One is greather then Date Two.");
}
}
CompareDate()
</script>
关于javascript - MVC - Javascript 在 View 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47888980/