asp.net mvc 入口控制到 kendo 完成的日期

asp.net mvc entry control to a date done with kendo

你好,我正在与 kendo 合作,我正在控制日期 但它没有这样做,当我输入一个我不输入的值时(比如 DateDebut>DateFinPrevue),它向我显示一个异常!!!

这是我的控制器:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using mvc_depences.Models;
namespace mvc_depences.Controllers
{
    public class ProjetController : Controller
    {
        private BD_GestionDepences db = new BD_GestionDepences();
        public ActionResult Index()
        {
            var projets = db.Projets.Include(p => p.U);
            return View(projets.ToList());
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(FormCollection form)
        {
            Projet projet = new Projet();
            int x=Convert.ToInt32(TempData.Peek("x").ToString());
            projet.UtilisateurID = x;
            string name = form["nom"].ToString();
            var n = (from p in db.Projets
                     where (p.nomP.Equals(name))
                     select p).FirstOrDefault();
            if (n != null)
                ViewBag.NomExiste = "Ce Nom De Projet Existe Déjà!";
            else
            {
                projet.nomP = form["nom"];
                if (form["DateDebut"].Trim().Length != 0)
                {
                    projet.DateDebut = Convert.ToDateTime(form["DateDebut"]);
                };
                if (form["DateFinPrevue"].Trim().Length != 0)
                {
                    projet.DateFinPrevue = Convert.ToDateTime(form["DateFinPrevue"]);
                };
                projet.DateFinReele = null;
                if (form["DateFinReele"].Trim().Length != 0)
                {
                    projet.DateFinReele = Convert.ToDateTime(form["DateFinReele"]);
                };
                projet.Description = form["Description"];
                projet.etat = form["etat"];
                if (ModelState.IsValid)
                {
                    db.Projets.Add(projet);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                };
            };
            return View(projet);
        }
        
        public string NomChefProjet( Projet projet)
        {
            return (from u in db.Utilisateurs
                    join p in db.Projets
                    on u.UtilisateurID equals p.UtilisateurID
                    where (u.UtilisateurID.Equals(projet.UtilisateurID) && projet.UtilisateurID.Equals(p.UtilisateurID))
                    select u.nom).FirstOrDefault();
        }

        public ActionResult Projet_Read([DataSourceRequest]DataSourceRequest request)
        {
            var projets = (from a in db.Projets
                          join b in db.Utilisateurs on a.UtilisateurID equals b.UtilisateurID
                          select new 
                          { 
                              ProjetId=a.ProjetId,
                              nomP=a.nomP,
                              DateDebut=a.DateDebut,
                              DateFinPrevue=a.DateFinPrevue,
                              DateFinReele=a.DateFinReele,
                              etat=a.etat,
                              Description=a.Description,
                              U=b.Prenom +" "+ b.nom
                          });
            DataSourceResult result = projets.ToDataSourceResult(request, projet1 => new
            {
                ProjetId = projet1.ProjetId,
                nomP = projet1.nomP,
                DateDebut = projet1.DateDebut,
                DateFinPrevue = projet1.DateFinPrevue,
                DateFinReele = projet1.DateFinReele,
                Description = projet1.Description,
                etat = projet1.etat,
                U=projet1.U,
            });
             return Json(result);
        }
        
        public ActionResult Projet_UpDate()
        {
            return View();
        }
    }
}

这是我的观点:

@model mvc_depences.Models.Projet
    @{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/admin.cshtml";
    }
        <link href="~/Content/bootstrap/css/bootstrap.css" rel="stylesheet" />
        <link href="~/Content/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
        <link rel="stylesheet" href="styles/kendo.common.min.css" />
        <link rel="stylesheet" href="styles/kendo.default.min.css" />
        <script src="js/jquery.min.js"></script>
        <script src="js/kendo.all.min.js"></script>

    @if (ViewBag.NomExiste != null)
    {
        <div class="alert alert-error" style="float: none;" role="dialog">@ViewBag.NomExiste</div>
    }
    @using (Html.BeginForm("Create", "Projet", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        <fieldset>
             <legend class="legend">Ajouter Un Nouveau Projet</legend>
            <div class="demo-section k-content">
                 <form id="CreateProject" data-role="validator" novalidate="novalidate">
                     <br />
                     <div class="panel">
                         <div class="panel-body">
                             <div class="col-md-6 col-sm-6">
                                 <div class="row">
                                     <div class="form-group">
                                         <div class="col-md-6 col-sm-6">
                                             <label class="cke_label" for="NomP">Nom</label>
                                         </div>
                                         <div class="col-md-6 col-sm-6">
                                             <input type="text" class="form-control" placeholder="Nom Projet" data-val="true" required="" data-val-length="Taille max  est 50 caracteres !! " data-val-length-max="50" id="nom" name="nom">
                                             <span class="field-validation-valid text-danger" data-valmsg-for="nom" data-valmsg-replace="true"></span>
                                         </div>
                                      </div> 
                                     <br /><br />
                                     <div class="row">
                                         <div>
                                             <div class="demo-section k-content">
                                                 <!--Date Debut !!-->
                                                 <div class="form-group">
                                                     <div class="col-md-6 col-sm-6">
                                                         <label class="cke_label" for="DateDebut">Date Debut</label>
                                                     </div>
                                                     <div class="col-md-6 col-sm-6">
                                                         <span class="k-widget k-datepicker k-header text" style="width: 245px;">
                                                             <span class="k-picker-wrap k-state-default">
                                                                 <input data-val="true" class="text k-input" required="" style="width: 100%;" data-val-date="The field DateDebut must be a date." data-val-required="Le champ Date Debut est requis." id="DateDebut" name="DateDebut" type="text" data-role="datepicker" role="combobox" aria-expanded="false" data-type="date" aria-owns="DateDebut_dateview" aria-disabled="false" aria-readonly="false">
                                                                 <span unselectable="on" class="k-select" role="button" aria-controls="DateDebut_dateview">
                                                                     <span unselectable="on" class="k-icon k-i-calendar">select</span>
                                                                     <span data-for='DateDebut' class='k-invalid-msg'></span>
                                                                 </span>
                                                             </span>
                                                         </span>
                                                         <script>
                                                     jQuery(function () {
                                                         jQuery("#DateDebut").kendoDatePicker({ "format": "yyyy-MM-dd", "min": new Date(1940, 0, 1, 0, 0, 0, 0), "max": new Date(2100, 11, 31, 0, 0, 0, 0) });
                                                     });
                                                         </script>
                                                         <span class="text" data-valmsg-for="DateDebut" data-valmsg-replace="true"></span>
                                                     </div>
                                                 </div>
                                                 <br /><br />
                                                 <!--Date fin Prevue !!-->
                                                 <div class="form-group">
                                                     <div class="col-md-6 col-sm-6">
                                                         <label class="cke_label" for="DateFinPrevue">Date Fin Prevue</label>
                                                     </div>
                                                     <div class="col-md-6 col-sm-6">
                                                         <span class="k-widget k-datepicker k-header text" style="width: 245px;">
                                                             <span class="k-picker-wrap k-state-default">
                                                                 <input data-val="true" class="text k-input" required="" style="width: 100%;" data-greaterdate-field="DateDebut" data-greaterdate-msg='la Date Fin prévue doit être inférieure à la date début du projet ! ' data-val-date="The field DateFinPrevue must be a date." data-val-required="Le champ DateFinPrevue est requis." id="DateFinPrevue" name="DateFinPrevue" type="text" data-role="datepicker" role="combobox" aria-expanded="false" aria-owns="DateFinPrevue_dateview" aria-disabled="false" aria-readonly="false">
                                                                 <span unselectable="on" class="k-select" role="button" aria-controls="DateFinPrevue_dateview">
                                                                     <span unselectable="on" class="k-icon k-i-calendar">select</span>
                                                                     <span data-for='DateFinPrevue' class='k-invalid-msg'></span>
                                                                 </span>
                                                             </span>
                                                         </span>
                                                         <script>
                                                     jQuery(function () {
                                                         jQuery("#DateFinPrevue").kendoDatePicker({ "format": "yyyy-MM-dd", "min": new Date(1940, 0, 1, 0, 0, 0, 0), "max": new Date(2100, 11, 31, 0, 0, 0, 0) });
                                                     });
                                                         </script>
                                                         <span class="text" required="" data-valmsg-for="DateFinPrevue" data-valmsg-replace="true"></span>
                                                     </div>
                                                 </div>
                                                 <br /><br />
                                                 <!--Date fin Reele !!-->
                                                 <div class="form-group">
                                                     <div class="col-md-6 col-sm-6">
                                                         <label class="cke_label" for="DateFinReele">Date Fin Reele</label>
                                                     </div>
                                                     <div class="col-md-6 col-sm-6">
                                                         <span class="k-widget k-datepicker k-header text" style="width: 245px;">
                                                             <span class="k-picker-wrap k-state-default">
                                                                 <input data-val="true" class="text k-input" style="width: 100%;" data-greaterdate-field="DateDebut" data-greaterdate-msg='la Date Fin Réelle doit être inférieure à la date début du projet ! ' data-val-date="The field DateFinReele must be a date." data-val-required="Le champ DateFinReele est requis." id="DateFinReele" name="DateFinReele" type="text" data-role="datepicker" role="combobox" data-type="date" aria-expanded="false" aria-owns="DateFinReele_dateview" aria-disabled="false" aria-readonly="false">
                                                                 <span unselectable="on" class="k-select" role="button" aria-controls="DateFinReele_dateview">
                                                                     <span unselectable="on" class="k-icon k-i-calendar">select</span>
                                                                     <span data-for='DateFinReele' class='k-invalid-msg'></span>
                                                                 </span>
                                                             </span>
                                                         </span>
                                                         <script>
                                                     jQuery(function () {
                                                         jQuery("#DateFinReele").kendoDatePicker({ "format": "yyyy-MM-dd", "min": new Date(1940, 0, 1, 0, 0, 0, 0), "max": new Date(2100, 11, 31, 0, 0, 0, 0) });
                                                     });
                                                         </script>
                                                         <span class="text" data-valmsg-for="DateFinReele" data-valmsg-replace="true"></span>
                                                     </div>
                                                 </div>
                                             </div>
                                             <script>
                                                 $(document).ready(function () {
                                                     function DateDebutChange() {
                                                         var DateDebutDate = DateDebut.value(),
                                                             DateFinPrevue = DateFinPrevue.value();
                                                         if (DateDebutDate) {
                                                             DateDebutDate = new Date(DateDebutDate);
                                                             DateDebutDate.setDate(DateDebutDate.getDate());
                                                             DateFinPrevue.min(DateDebutDate);
                                                         } else if (DateFinPrevueDate) {
                                                             DateDebut.max(new Date(DateFinPrevueDate));
                                                         } else {
                                                             DateFinPrevueDate = new Date();
                                                             DateDebut.max(DateFinPrevuedDate);
                                                             DateFinPrevue.min(DateFinPrevueDate);
                                                         }
                                                     }
                                                     function DateFinPrevueChange() {
                                                         var DateFinPrevueDate = DateFinPrevue.value(),
                                                             DateDebutDate = DateDebut.value();
                                                         if (DateFinPrevueDate) {
                                                             DateFinPrevueDate = new Date(DateFinPrevueDate);
                                                             DateFinPrevueDate.setDate(DateFinPrevueDate.getDate());
                                                             DateDebut.max(DateFinPrevueDate);
                                                         } else if (DateDebutDate) {
                                                             DateFinPrevue.min(new Date(DateDebutDate));
                                                         } else {
                                                             DateFinPrevueeDebut.max(DateFinPrevueDate);
                                                             DateFinPrevue.min(DateFinPrevueDate);
                                                         }
                                                     }
                                                     var DateDebut = $("#DateDebut").kendoDatePicker({
                                                         change: DateDebutChange
                                                     }).data("kendoDatePicker");
                                                     var DateFinPrevue = $("#DateFinPrevue").kendoDatePicker({
                                                         change: DateFinPrevueChange
                                                     }).data("kendoDatePicker");
                                                     DateDebut.max(DateFinPrevue.value());
                                                     DateFinPrevue.min(DateDebut.value());
                                                 });
                                             </script>
                                         </div>
                                         <!-- Description -->
                                         <br /><br />
                                         <div class="form-group">
                                             <div class="col-md-6 col-sm-6">
                                                 <label class="cke_label" for="Description">Description</label>
                                             </div>
                                             <div class="col-md-6 col-sm-6">
                                                 <textarea class="form-control" rows="6" cols="35" id="Description" name="Description"></textarea>
                                             </div>
                                         </div>
                                         <br /><br />
                                         <div class="form-group">
                                             <div class="col-md-6 col-sm-6">
                                                 <label class="cke_label" for="etat">Etat</label>
                                             </div>
                                             <div class="input-group">
                                                 <div align="justify">
                                                     <br>
                                                     @Html.RadioButtonFor(m => m.etat, "En Cours", new { @checked = true })
                                                     <label class="etat" for="En_Cours"> En Cours</label>
                                                     <br />
                                                     @Html.RadioButtonFor(m => m.etat, "Termine")
                                                     <label class="etat" for="Termine">Terminé</label>
                                                     <br />
                                                     @Html.RadioButtonFor(m => m.etat, "Annule")
                                                     <label class="etat" for="Annule"> Annulé</label>
                                                     <br />
                                                 </div>
                                             </div>
                                         </div>
                                         <div class="row" style="margin-left:400px;margin-top:120px">
                                             <div class="col-md-12" style="width:350px">
                                                 <input id="Submit" data-role="button" class="btn btn-default" name="actionType" type="submit"  value="Engeristrer" style="width:150px" />
                                             </div>
                                         </div>
                                     </div>
                                 </div>
                             </div>
                         </div>
                     </div>
                 </form>
            </div>
        </fieldset>
    }

我的异常跟踪: System.FormatException 未被用户代码处理 HResult=-2146233033 Message=La chaîne n'a pas été reconnue en tant que DateTime valide。 Source=mscorlib StackTrace: à System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles 样式) à System.Convert.ToDateTime(字符串值) à mvc_depences.Controllers.ProjetController.Create(FormCollection 表单) dans c:\Users\NAD-info\Documents\Visual Studio 2013\Projects\mvc_depences\mvc_depences\mvc_depences\Controllers\ProjetController.cs:ligne 43 à lambda_method(Closure, ControllerBase, Object[]) à System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object[]参数) à System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) à System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2个参数) à System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod() à System.Web.Mvc.Async.AsyncControllerActionInvoker.b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) à System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult2.CallEndDelegate(IAsyncResult asyncResult) à System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End() à System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, 对象标签) à System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) à System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3d() à System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.b__3f() 内部异常:

实际上这就是我所做的! 我在控制器中转换了我的日期并且有效

这是我的代码:

public ActionResult Create(FormCollection form) {
  Projet projet = new Projet();
  int x = Convert.ToInt32(TempData.Peek("x").ToString());
  projet.UtilisateurID = x;
  string name = form["nom"].ToString();
  var n = (from p in db.Projets where(p.nomP.Equals(name)) select p).FirstOrDefault();
  if (n != null)
    ViewBag.NomExiste = "Ce Nom De Projet Existe Déjà!";
  else {
    projet.nomP = form["nom"];
    if (form["DateDebut"].Trim().Length != 0) {
      string s = form["DateDebut"];
      DateTime DateDebut1 = DateTime.ParseExact(s, "M/d/yyyy", CultureInfo.InvariantCulture);
      projet.DateDebut = DateDebut1;
    };
    if (form["DateFinPrevue"].Trim().Length != 0) {
      string s = form["DateFinPrevue"];
      DateTime DateFinPrevue1 = DateTime.ParseExact(s, "M/d/yyyy", CultureInfo.InvariantCulture);
      projet.DateFinPrevue = DateFinPrevue1;
    };
    projet.DateFinReele = null;
    if (form["DateFinReele"].Trim().Length != 0) {
      string s = form["DateFinReele"];
      DateTime DateFinReele1 = DateTime.ParseExact(s, "M/d/yyyy", CultureInfo.InvariantCulture);
      projet.DateFinReele = DateFinReele1;
    };
    projet.Description = form["Description"];
    projet.etat = form["etat"];
    if (ModelState.IsValid) {
      db.Projets.Add(projet);
      db.SaveChanges();
      return RedirectToAction("Index");
    };
  };
  return View(projet);
}

感谢您的评论!