2016-12-31 13 views
0

Ich versuche, Änderungen in einem grundlegenden CRUD zu speichern. Ich habe meine Ansicht für 3 Spalten in meinem Modell bearbeitet (Tabelle hat 7 Spalten).Nicht speichern nach nach Bearbeitungsaktion

Ich versuchte Attach-Methode, die in einem anderen Beitrag verwiesen wurde, der nicht funktioniert hat. Irgendwelche Gedanken würden geschätzt werden.

Modell

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; } 
} 

}

-Controller

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"); 
    } 
} 

Ansicht

@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

Wir müssen die Definition von 'AssetRequest' mit allen Attributen und/oder dem Modelbuilder-Code für Entity Framework sehen. Wir brauchen auch den Wert von 'db.SaveChangesAsync()' –

+0

Können Sie Code von .saveChangeAsync-Methode hinzufügen? –

+2

@AidaIsay ['SaveChangesAsync()'] (https://msdn.microsoft.com/en-us/library/dn220070 (v = vs.113) .aspx) ist eine Framework-Methode, für die es keinen * code * gibt es. –

Antwort

2

Sie müssen die Id-Eigenschaft (die der Primärschlüssel ist) in die Include-Liste aufnehmen, damit EF das Element abrufen und aktualisieren kann.

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

Offenbar verwenden Sie Ihr Entitätsmodell als Parameter zum Aktualisieren der Entitätswerte. A better approach to prevent over posting is to use a view model.

+0

@ Shyju: Ich möchte es nur drei Eigenschaftsnamen absichtlich zu aktualisieren. Meine Frage ist, warum sie diese nicht aktualisiert und die Ansicht an alle zurückgibt. – moka

+0

Danke, es funktioniert immer noch nicht, – moka

+0

erhalten Sie irgendwelche Fehler? Haben Sie die Tabellendaten überprüft, um zu bestätigen, dass sie nicht aktualisiert werden? Ihr Code sieht gut aus (solange Sie die ID angeben). – Shyju

Verwandte Themen