【问题标题】:Not saving to after Edit action编辑操作后不保存
【发布时间】:2017-01-01 07:47:42
【问题描述】:

我正在尝试将更改保存到基本 CRUD 中。我已经编辑了模型中 3 列的视图(表格有 7 列)。

我尝试了在另一个帖子中引用的附加方法,但它不起作用。任何想法将不胜感激。

型号

    public class AssetRequest
{
    public int Id { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Request date")]
    public DateTime AssetRequestDate { get; set; }



    [Display(Name = "Facility")]
    public int FacilityId { get; set; }

    [Required]
    [Display(Name = "Asset requested")]
    public int AssetId { get; set; }

    [Display(Name ="Serial no.")]
    public string AssetSN { get; set; }

    [Required]
    [Display(Name = "Request type")]
    public int RequestTypeId { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Job date")]
    public DateTime JobRequestDate { get; set; }



    [Required]
    [Display(Name = "Request status")]
    public int RequestStatusId { get; set; }



    [Display(Name = "Tracking no.")]
    public string TrackingNo { get; set; }

    [Display(Name = "Comments")]
    public string Comments { get; set; }

    [Display(Name = "Sending facility")]
    public string SendingFacilityt { get; set; }

    public virtual Asset Asset { get; set; }
    public virtual Facility Facility { get; set; }
    public virtual ApplicationUser User { get; set; }
    public virtual RequestType RequestType { get; set; }

    public virtual RequestStatus RequestStatus { get; set; }
}

}

控制器

public async Task<ActionResult> Edit([Bind(Include = "RequestStatusId, TrackingNo, Comments")] AssetRequest assetRequest)
{
    if (ModelState.IsValid)
    {

        //db.AssetRequestTable.Attach(assetRequest);
        db.Entry(assetRequest).State = EntityState.Modified;
        await db.SaveChangesAsync();
        return RedirectToAction("All");
    }
}

查看

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>AssetRequest</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)



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

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



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

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

        <div class="form-group">
            @Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Comments, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Comments, "", 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>
}

<div>
    @Html.ActionLink("Back to List", "All")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

【问题讨论】:

  • 我们需要查看AssetRequest 的定义,包括实体框架的任何属性和/或模型构建器代码。我们还需要从db.SaveChangesAsync()返回的值
  • 你能从 .saveChangeAsync 方法中添加代码吗?
  • @AidaIsay SaveChangesAsync() 是一个框架方法,它没有代码
  • @Erik,抱歉,不知道为什么复制时没有添加其余部分

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


【解决方案1】:

您需要将 Id 属性(即主键)包含到 Include 列表中,以便 EF 可以获取该项目并对其进行更新。

public async Task<ActionResult> Edit([Bind(Include = "Id,RequestStatusId, TrackingNo, 
                                                   Comments")] AssetRequest assetRequest)
{
   // your code
}

看起来您正在使用实体模型作为参数来更新实体值。 A better approach to prevent over posting is to use a view model.

【讨论】:

  • @Shyju:我想让它故意只更新三个属性名。我的问题是为什么它不更新这些并将视图返回给所有人。
  • 您是否遇到任何错误?您是否检查了表格数据以确认它没有更新?您的代码看起来不错(只要包含 Id)。
  • db 上什么都没有,我希望它更新,只是不返回视图,我正在重写以删除 async 并查看它给出了什么
猜你喜欢
  • 2011-01-12
  • 2017-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多