2016-12-31 56 views
0

我試圖保存對基本CRUD的更改。我在我的模型中編輯了3列的視圖(表格有7列)。未保存到後編輯操作

我試過在不同的帖子中引用的attach方法,這個方法沒有工作。任何想法將不勝感激。

模型

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") 
} 
+2

我們需要查看'AssetRequest'的定義,包括實體框架的任何屬性和/或模型構建器代碼。我們還需要從'db.SaveChangesAsync()'返回的值' –

+0

您可以從.saveChangeAsync方法添加代碼嗎? –

+2

@AidaIsay ['SaveChangesAsync()'](https://msdn.microsoft.com/en-us/library/dn220070(v = vs.113).aspx)是一個框架方法,沒有* code * for它。 –

回答

2

您需要將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

+0

@ Shyju:我希望它只是故意更新三個屬性名稱。我的問題是爲什麼不更新這些視圖並將視圖返回給所有人。 – moka

+0

謝謝,它仍然不能正常工作, – moka

+0

你有什麼錯誤嗎?你檢查了表格數據以確認它沒有更新嗎?你的代碼看起來很好(只要你包含Id)。 – Shyju