我正在对用户所引用的日期进行输入控制 我的视图代码工作得很好但是当它进入控制器时它无法将属性转换为日期! 这是我的控制器方法:
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);
}
这是我观点的一部分:
<!--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(),
DateFinPrevueDate = 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(DateFinPrevueDate);
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 {
DateFinPrevueDate = new Date();
DateDebut.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>
答案 0 :(得分:0)
这是我的控制器方法:
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);
}
&#13;
非常感谢你的帮助!