0
名前と2つの日付が返されるはずですが、日付は常にnullになるようです。日付の入力は常にオブジェクトにnullを返します
jsが入力フィールドから読み込む日付を表示するために、作成ボタンにアラートをバインドしましたが、それは常に正しいものでしたが、ポストアクションに戻ると、オブジェクトは常にStartedOnとFinishedOnにnull値を持ちます(もし日付であれば、それは私に日付として01/01/2001を与えます。データベースに保存しようとすると例外がスローされます) "datetime2データ型をdatetimeデータ型に変換すると、out-of-範囲値」)。
私の質問は、nullableまたは01/01/2001の場合は常にnullを返し、どうすれば修正できますか?
マイビュー:
@model Data.Project
@{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Project</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartedOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartedOn, new { htmlAttributes = new {@class = "form-control",type="date" } })
@Html.ValidationMessageFor(model => model.StartedOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FinishedOn, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FinishedOn, new { htmlAttributes = new {@class = "form-control" , type = "date" } })
@Html.ValidationMessageFor(model => model.FinishedOn, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" id="11" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section scripts{
<script>
$(function() {
$("#11").on("click",function() {
alert($("#StartedOn").val());
})
})
</script>
}
私のアクション:(ここでは日付がallredy NULLである)
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name")] Project project)
{
if (ModelState.IsValid)
{
UtilityContext.AddProject(project);
UtilityContext.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
マイモデル:
public class Project
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public DateTime? StartedOn { get; set; }
public DateTime? FinishedOn { get; set; }
[ForeignKey("ProjectId")]
public virtual ICollection<Employee> EmployeesOnIt { get; set; }
}
今はかなり愚かな気がする。ありがとう。 – kokosaish
私は同じことを何回もやりました。それは起こる。 –