我試圖將'拍賣'變量中的信息持續存儲到尚未創建的表'拍賣'中。實體框架應該爲我創建它我明白。正如你所看到的,我的程序已經達到了表格數據包含在「拍賣」變量中的程度。但是你可以看到調試器在'db.Auction.Add(auction)'處停止。通過ASP.NET將數據持久化到SQL Server MVC
爲什麼程序不會讓數據庫將「auction」變量中的數據添加到Auction表中?
我會很感激。 謝謝, 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'至少包含一個行數據插入)。還包括您的視圖代碼,模型綁定,並避免發佈代碼邏輯作爲圖像截圖。 –
動作已經作爲DbSet的DbContext類的一部分。代碼只能是'db.Add(auction);' – Edward
請看下面我的回覆。謝謝。 –