如何在razor mvc中比较两个日期?

时间:2019-04-27 16:22:41

标签: c# razor

给出以下代码:

我的剃须刀代码是这个:

<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的方法吗?谢谢

1 个答案:

答案 0 :(得分:0)

使用这个我希望有用:

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