javascript - MVC - Javascript 在 View 中如何工作?

标签 javascript asp.net-mvc razor

很抱歉我的问题,但我是 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/

相关文章:

c# - asp.net 身份 SetEmailConfirmedAsync

asp.net-mvc - ASP.NET MVC 路由和静态数据(即图像、脚本等)

asp.net-mvc-3 - 在 MVC3 中创建默认站点页面标题

asp.net-mvc-3 - 在 Razor/MVC3 中显示来自数据库的图像

c# - 我的局部 View 每次都会导致我的应用程序崩溃

javascript - 单击使用 raphael js 在另一个 div 中淡入淡出

javascript - jQuery 1.7.2 - selector.val() 在 IE 8 中中断

javascript - 如何测试两个 HTML 元素或 jQuery 选择器之间的等价性?

名称中带有 "-"的 Javascript 样式变量无法更改?

c# - 如何在 MVC 应用程序中处理多种语言?