2017-04-21 17 views
-1

「オークション」変数の情報をまだ作成されていないテーブル「オークション」に保存しようとしています。エンティティのフレームワークは私が理解するためにそれを作成することになっています。ご覧のように、私のプログラムはフォームデータが 'オークション'変数に含まれるようにしました。しかし、あなたが見ることができるように、デバッガは 'db.Auction.Add(オークション)'で停止しています。ASP.NET MVCを使用してSQL Serverにデータを永続化する

dbに 'オークション'変数のデータをオークションテーブルに追加させることで、プログラムが進まないのはなぜですか?

私はそれを感謝します。 ありがとう、 CM これまでの返信いただきありがとうございますが、提案は機能していません。私は自分のコードで書いただけでなく、このスレッドを開始したのと同じメッセージであるエラーメッセージを再び表示しました。 enter image description here

ビュー

@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(); 
    } 
+0

この例外は 'auction'内に配列やリスト要素がないことを意味します(' Models.Auction'に少なくとも挿入する行データが含まれているかどうかを確認してください)。また、あなたのビューコード、モデルバインディングを含み、イメージのスクリーンショットとしてコードロジックを投稿するのを避けてください。 –

+0

アクションは、DbContextクラスのDbSetとしてすでに離れています。コードは 'db.Add(オークション);でなければなりません。 – Edward

+0

下記の返信をご覧ください。ありがとう。 –

答えて

0

アクションは、DbContextとしてDbContextクラスから既に離れています。また、ビューは既にモデル情報を、オーバーロードされたパラメータとして返す必要はありません。投稿を変更して

public class AuctionsController : Controller 
{ 
    private readonly AuctionsDataContext_context; 

    public AuctionsController(AuctionsDataContext context) 
    { 
     _context = context;  
    } 

    public ActionResult Create (Bind[("Id,Title,StartDate,EndDate,DeliveryCost,StartBid,BuyNowPrice,BuyNowEnabled,Description")] Auction auction) 
    { 
     if (ModelState.IsValid) 
     { 
      _context.Add(auction); 
      await _context.SaveChangesAsync(); 
      return RedirectToAction("Index"); 
     } 
     return View(); 
    } 
+0

更新された返信をご覧ください。ありがとう。 –

+0

更新された返信ではない@ChrisMazzochi、それは答えではない回答です。また、あなたは推奨事項のどれもがうまくいかないと主張しますが、それが何を説明するのに失敗します。 – Edward

+0

@ChrisMazzochiあなたがコメントをしたところで私は自分自身を繰り返していました。私の答えを更新し、私が提案した変更を加えるときにあなたが戻ってきたことを説明してください。 – Edward

関連する問題