2017-12-19 4 views
1

Es tut mir leid für meine Frage, aber ich bin neu in MVC.MVC - Wie funktioniert Javascript in Sicht?

Das ist meine Situation.

Aus meiner Sicht, ich habe ein Modell (@model DAEF.Models.M_Generic)

und ich möchte 2 Felder vergleichen. Meine Frage ist, wie kann ich das Javascript

für das tun?

Unter meinem Beispiel Code mit 2 Daten.

@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>  
} 

Antwort

1

Da Sie die Eigenschaften des Modells vergleichen, benötigen Sie keine Javascript-Variablen.

function CompareDate() { 
    @if (model.Dat_End > model.Dat_Start) { 
    alert("Date One is greather then Date Two."); 
    } 
} 
CompareDate(); 
0

Sie ToShortDateString() verwenden können und konvertieren diese Zeichenfolge in JavaScript Date wie:

<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> 
Verwandte Themen