2017-04-21 28 views
-1

我試圖將'拍賣'變量中的信息持續存儲到尚未創建的表'拍賣'中。實體框架應該爲我創建它我明白。正如你所看到的,我的程序已經達到了表格數據包含在「拍賣」變量中的程度。但是你可以看到調試器在'db.Auction.Add(auction)'處停止。通過ASP.NET將數據持久化到SQL Server MVC

爲什麼程序不會讓數據庫將「auction」變量中的數據添加到Auction表中?

我會很感激。 謝謝, 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

動作已經作爲DbSet的DbContext類的一部分。代碼只能是'db.Add(auction);' – Edward

+0

請看下面我的回覆。謝謝。 –

回答

0

動作已經作爲DbSet的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

相關問題