首页 > 解决方案 > 如何比较 razor mvc 中的两个日期?

问题描述

给定以下代码:

我的剃刀代码是这个:

<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
     <tr>
        <td>Date: </td>
        <td>
            <div class="form-group input-group-sm">
                @Html.LabelFor(model => model.StartDate)
                @Html.TextBoxFor(model => model.StartDate, new { @class = "form-control datepicker", placeholder = "Enter Drop-off date here..." })
                @Html.ValidationMessageFor(model => model.StartDate)
            </div>
        </td>
    </tr>

    <tr>
        <td>end date: </td>
        <td>
            <div class="form-group input-group-sm">
                @Html.LabelFor(model => model.EndDate)
                @Html.TextBoxFor(model => model.EndDate, new { @class = "form-control datepicker", placeholder = "Enter Drop-off date here..." })
                @Html.ValidationMessageFor(model => model.EndDate)
            </div>
        </td>
    </tr>
 </table>
 }

我的模型是这个:

public class MyModel
{
    [Display(Name = "From")]
    [Required]
    public DateTime StartDate { get; set; }

    [Display(Name = "To")]
    [Required]
    public DateTime EndDate { get; set; }

}

你能给我一个方法来检查 StartDate 是否低于 EndDate 吗?谢谢

标签: c#razor

解决方案


使用这个我希望有用:

  @if (Model.StartDate < Model.EndDate )
  {
      //Do something
  }

推荐阅读