0
私は単純なCRUDで以下の変更を成功させようとしています。C#モデル 'db.SaveChanges'の前にプロパティを変更する
APPには、2つのモデル、2つのコントローラ、および各コントローラのCRUDビューが含まれています。
「店舗」および「製品」コントローラ。
<div class="form-horizontal">
<h4>Store</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Notes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Notes, "", new { @class = "text-danger" })
</div>
</div>
あなたが見ることができるように、3つのプロパティ、場所、説明及び注:私は
を作成「作成」ビューには、以下を持つ新しいストアを作成する必要がありますし、その新しい店に関連する製品を作成します。コントローラコード:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Location,Description,Notes,StoreCode")] Store store)
{
if (ModelState.IsValid)
{
db.Stores.Add(store);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(store);
}
私は
StoreCode.DateTime.Now.ToFileTime();
としての私のデータベースに挿入するには、このバインドモデルに「ストアコード」と呼ばれる第四プロパティを追加しようとしているが、私は、ユーザーにしたくありませんこのビューをビューに作成します。私は自動的にこの値をデータベースに割り当てたいと思います。私は正しい方向に進んでいますか?ありがとう
この 'バインド(= "場所、説明、注意事項" を含める)'そして 'db.Stores.Add(店舗)この' store.StoreCodeような '何かする前にそれを設定するような' StoreCode'を含めるする必要はありません= "yourcode" ' – RizkiDPrast