ユーザーデータを受け入れ、データベースに追加するアプリケーションがあります。データが正常に追加された場合は、成功メッセージが表示されます。しかし、私はそれをテストすると、私は成功メッセージが表示されますが、データはデータベースに追加されていません。モデル状態は有効であり、成功メッセージは表示されますが、データはデータベースに追加されません。
最初にEntity Frameworkを使用してビューを作成しました。もともとモデルにはIsCovered
フィールドが含まれていませんでした。私はそれを後で追加し、モデルをデータベースから更新しました。私はそれをした後、データをデータベースに追加することをやめました。ここで
は私のコントローラである:ここでは
// POST: Shifts/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ShiftID,Date,StartTime,EndTime,StoreNum,Id,IsCovered")] Shift shift)
{
if (ModelState.IsValid)
{
shift.Id = User.Identity.GetUserId();
shift.StoreNum = User.StoreNum();
db.SaveChanges();
TempData["Success"] = "Shift posted for " + shift.Date.ToString("mm/dd/yyyy") + " at store #" + shift.StoreNum + " successfully.";
return RedirectToAction("Create", "Shifts");
}
return View(shift);
}
は私のモデルである:
public partial class Shift
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Shift()
{
this.CoveredShifts = new HashSet<CoveredShift>();
}
public int ShiftID { get; set; }
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode =true)]
public System.DateTime Date { get; set; }
[DisplayFormat(DataFormatString = "{0:HH:mm tt}", ApplyFormatInEditMode = true)]
[DataType(DataType.Time)]
[Display(Name="Start Time")]
public System.DateTime StartTime { get; set; }
[DisplayFormat(DataFormatString = "{0:HH:mm tt}", ApplyFormatInEditMode = true)]
[DataType(DataType.Time)]
[Display(Name ="End Time")]
public System.DateTime EndTime { get; set; }
[Display(Name ="Store Number")]
public string StoreNum { get; set; }
public string Id { get; set; }
public bool IsCovered { get; set; }
public virtual AspNetUser AspNetUser { get; set; }
public virtual Store Store { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<CoveredShift> CoveredShifts { get; set; }
}
そしてここでは、私の見解である:
@model SSM_V5.Models.Shift
@{
ViewBag.Title = "Post a Shift";
}
@using Microsoft.AspNet.Identity
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.3/themes /smoothness/jquery-ui.css" type="text/css" />
<script src="https://code.jquery.com/jquery-2.2.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-timepicker/1.8.1/jquery.timepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery-timepicker/1.8.1/jquery.timepicker.min.css" type="text/css" />
<script language="javascript" type="text/javascript">
$(function() {
$('.datepicker').datepicker({
showOptions: { speed: 'fast' },
changeMonth: true,
changeYear: true,
dateFormat: 'MM/dd/yy',
gotoCurrent: true
});
$('.timepicker').timepicker();
});
</script>
</head>
<h2>Post a Shift</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@if (TempData["Success"] != null)
{
<p class="alert alert-success" id="successMessage">@TempData["Success"]</p>
}
<h4>Please add your shift details below.</h4>
<hr />
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.StoreNum)
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control datepicker" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10" >
@Html.EditorFor(model => model.StartTime, new { htmlAttributes = new { @class = "form-control timepicker" } })
@Html.ValidationMessageFor(model => model.StartTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndTime, new { htmlAttributes = new { @class = "form-control timepicker" } })
@Html.ValidationMessageFor(model => model.EndTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-8">
<input type="submit" value="Post Shift" class="btn btn-success" />
</div>
</div>
</div>
この問題に任意の洞察力が高く評価され、通り私のコードについての一般的なフィードバックもあります。
うわー...私はそれを見落として信じられない!ご協力いただきありがとうございます! –
問題ありません!あなたは自分自身と一緒に(そして私は他の人には確信している)このようなことを何度も見落としてしまった!この質問に答えがあるように、この回答を受け入れたものとしてマークしてください。ハッピーコーディング! –