「オークション」変数の情報をまだ作成されていないテーブル「オークション」に保存しようとしています。エンティティのフレームワークは私が理解するためにそれを作成することになっています。ご覧のように、私のプログラムはフォームデータが 'オークション'変数に含まれるようにしました。しかし、あなたが見ることができるように、デバッガは 'db.Auction.Add(オークション)'で停止しています。ASP.NET MVCを使用してSQL Serverにデータを永続化する
dbに 'オークション'変数のデータをオークションテーブルに追加させることで、プログラムが進まないのはなぜですか?
私はそれを感謝します。 ありがとう、 CM これまでの返信いただきありがとうございますが、提案は機能していません。私は自分のコードで書いただけでなく、このスレッドを開始したのと同じメッセージであるエラーメッセージを再び表示しました。
ビュー
@model MvcAuction.Models.Auction
@{
ViewBag.Title = "CreateAuctionItem";
}
<h2>@ViewBag.Title.</h2>
<div id="createAuctionItemSection">
@using (Html.BeginForm("Create", "Auctions", FormMethod.Post,
new { @class = "form-horizontal", @id =
"registerForm", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Create An Item For Auction.</h4>
<hr />
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Title, new { @class = "col-md-2 control-
label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Title, new { @class = "form-control", @id = "title" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.StartDate, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.StartDate, "{0:yyyy-MM-dd}", new { type = "date" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.EndDate, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.EndDate, "{0:yyyy-MM-dd}", new { type = "date" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.DeliveryCost, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.DeliveryCost, new { @class = "form-control", @id = "deliveryCost" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.StartBid, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.StartBid, new { @class = "form-control", @id = "startBid" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.BuyNowPrice, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.BuyNowPrice, new { @class = "form-control", @id = "buyNowPrice" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.BuyNowEnabled, new { @Value = "Show Buy Now Price?", @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.CheckBoxFor(m => m.BuyNowEnabled, new { @class = "form-control", @id = "buyNowEnabled" })
</div>
</div>
@Html.ValidationSummary("", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.Description, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.Description, new { @class = "form-control", @id = "description" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" class="btn btn-default" value="Create Item" />
</div>
</div>
}
<img src="~/Content/Images/progress.gif" id="progress" style="display:none;" />
<h3>@ViewBag.TheMessage</h3>
</div><!--End createAuctionItemSection-->
モデル
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MvcAuction.Models
{
public class Auction
{
public long Id { get; set; }
[Required]
[Column(TypeName = "varchar")]
[Display(Name = "Title")]
public String Title { get; set; }
public string ImageURL { get; set; }
[Required]
[Column(TypeName = "date")]
[Display(Name = "Start Date")]
public DateTime StartDate { get; set; }
[Required]
[Column(TypeName = "date")]
[Display(Name = "End Date")]
public DateTime EndDate { get; set; }
[Required]
[Column(TypeName = "decimal")]
[Display(Name = "Delivery Cost")]
public decimal DeliveryCost { get; set; }
[Required]
[Column(TypeName = "decimal")]
[Display(Name = "Start Bid")]
public decimal StartBid { get; set; }
[Column(TypeName = "decimal")]
[Display(Name = "Buy Now Price")]
public decimal BuyNowPrice { get; set; }
[Column(TypeName = "bool")]
[Display(Name = "Buy Now Enabled")]
public Boolean BuyNowEnabled { get; set; }
[Column(TypeName = "varchar")]
[Display(Name = "Description")]
public String Description { get; set; }
[Column(TypeName = "int")]
[Display(Name = "View Count")]
public int ViewCount = 0;
public decimal? getCurrentTopBid()
{
return StartBid;
}
}
}
コントローラのアクション
[HttpPost]
public ActionResult Create(Auction auction)
{
if (ModelState.IsValid)
{
var db = new AuctionsDataContext();
db.Auction.Add(auction);
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
この例外は 'auction'内に配列やリスト要素がないことを意味します(' Models.Auction'に少なくとも挿入する行データが含まれているかどうかを確認してください)。また、あなたのビューコード、モデルバインディングを含み、イメージのスクリーンショットとしてコードロジックを投稿するのを避けてください。 –
アクションは、DbContextクラスのDbSetとしてすでに離れています。コードは 'db.Add(オークション);でなければなりません。 – Edward
下記の返信をご覧ください。ありがとう。 –