【问题标题】:C# ASP.NET MVC 5 Entity Framework - edit function did not workC# ASP.NET MVC 5 Entity Framework - 编辑功能不起作用
【发布时间】:2017-04-08 12:55:33
【问题描述】:

这是我的模型:

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 时,它会将数据检索到视图中,但开始时间除外。而且我不能提交详细信息,这会导致异常。

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

我在这里显示屏幕截图:

enter image description here

即使我在我的视图中提交带有给定详细信息的开始日期,它也会引发异常

截图显示:

enter image description here

EntityFramework.dll 中出现“System.Data.Entity.Infrastructure.DbUpdateConcurrencyException”类型的异常,但未在用户代码中处理

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

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

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

我猜问题出在 ID 上...当我调试程序时它被传递 0 到控制器...如何更改这个?...

【问题讨论】:

  • db.Entry(auction).State = EntityState.Modified; db.SaveChanges(); - 这就是控制器让我关注的地方
  • 请帮我解决这个问题
  • 你能在 Edit(POST) 方法中设置断点,看看是否为 'auction' 对象正确设置了 Id 吗?
  • 尝试对StartTime 使用与EndTime 相同的DisplayFormat 属性(这似乎有效)。
  • @Boney - 我去看看......

标签: c# entity-framework asp.net-mvc-5


【解决方案1】:

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

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

改成

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

【讨论】:

    【解决方案2】:

    首先你的开始时间 格式注释“{0:yyyy MM, dd}”,看起来“{ 0:yyyy-MM-dd}" 为真。之后由于您的 [DataType(DataType.Currency)] 注释和传输此字段数据的问题,发生了此异常。

    【讨论】:

    猜你喜欢
    • 2012-08-10
    • 2020-09-17
    • 1970-01-01
    • 2020-03-29
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多