C# ASP.NET MVC 5 实体框架 - 编辑功能不起作用



这是我的模型:

public class Auction
{
[Key]
[Required]
public long Id { get; internal set; }
[Required]
[Display(Name = "Title")]
public string Title { get; set; }
[Required]
[Display(Name = "Description")]
public string Description { get; set; }
[Required]
[Display(Name = "Product Name")]
public string productName { get; set; }
[Required]
[Display(Name = "Product Price")]
public string productPrice { get; set; }
[Required]
[Display(Name = "Auction Start Time")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy MM, dd}", ApplyFormatInEditMode = true)]
public DateTime StartTime { get; set; }
[Required]
[Display(Name = "Auction End Time")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime EndTime { get; set; }
[Required]
[DataType(DataType.Currency)]
[Display(Name = "Auction Start Price")]
public decimal StartPrice { get; set; }
[DataType(DataType.Currency)]
[Display(Name = "Auction Current Price")]
public decimal? CurrentPrice { get; set; }
}
}

这是我DataContext课:

public class AuctionsDataContext : DbContext
{
public DbSet<Auction> Auctions { get; set; }
static AuctionsDataContext()
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<AuctionsDataContext>());
}
}

这是我的观点:

@model Mohn.Models.Auction
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Auction</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", 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.productName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.productName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.productName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.productPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.productPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.productPrice, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
@Html.LabelFor(model => model.StartTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndTime, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.EndTime, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndTime, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.StartPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CurrentPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CurrentPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CurrentPrice, "", new { @class = "text-danger" })
</div>
</div>

<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}

这是我的编辑部分,在控制器中不起作用:

public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Auction auction = db.Auctions.Find(id);
if (auction == null)
{
return HttpNotFound();
}
return View(auction);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Title,Description,productName,productPrice,StartTime,EndTime,StartPrice,CurrentPrice")] Auction auction)
{
if (ModelState.IsValid)
{
db.Entry(auction).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(auction);
}

问题是,当我在 URL 中传递Auction id时,它将检索除开始时间之外的视图。 而且我无法提交详细信息,这会导致异常。

开始时间不会显示在我的视图中,

我在这里显示屏幕截图:

在此处输入图像描述

即使我提交开始日期并在我的视图上提供了给定的详细信息,它也会引发异常

屏幕截图显示:

在此处输入图像描述

EntityFramework 中发生了类型为"System.Data.Entity.Infrastructure.DbUpdateConcurrencyException"的异常.dll但未在用户代码中处理

其他信息:存储更新、插入或删除语句影响意外的行数 (0)。实体可能已被修改或删除

请帮我解决这个问题,我是MVC 5 Asp.net 实体框架的新手,我想知道到底发生了什么......

感谢您的关注...和帮助..:)

我想问题出在 ID 上...当我调试程序时,它被传递给控制器 0...如何改变这一点?...

您需要删除 setter 方法 id 属性的内部属性。由于内部模型绑定器无法为其设置值

[Key]
[Required]
public long Id { get; internal set; }

将其更改为

[Key]
[Required]
public long Id { get;  set; }

首先,您的开始时间格式注释"{0:yyyyMM,dd}",这似乎是"{0:yyyy-MM-dd}">是真的。 之后,由于您的[数据类型(数据类型.货币)]注释和传输此字段数据的问题,发生了此异常。

最新更新