0

I am trying to save changes to in a basic CRUD. I have edited my view for 3 columns in my model (table has 7 columns).

I tried attach method which was referenced in a different post which did not work. Any thoughts would be appreciated.

Model

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

View

@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")
}
moka
  • 35
  • 1
  • 6
  • 2
    We'll need to see the definition of `AssetRequest` including any attributes and/or the modelbuilder code for Entity Framework. We'll also need the value returned from `db.SaveChangesAsync()` – Erik Philips Dec 31 '16 at 22:05
  • Can you add code from .saveChangeAsync method? – AI I Dec 31 '16 at 22:05
  • 2
    @AidaIsay [`SaveChangesAsync()`](https://msdn.microsoft.com/en-us/library/dn220070(v=vs.113).aspx) is a framework method, there is no *code* for it. – Erik Philips Dec 31 '16 at 22:06
  • @Erik, apologies, not sure why it didnt add the rest when copied – moka Jan 01 '17 at 00:58

1 Answers1

2

You ned to include the Id property (which is the primary key) to the Include list so that EF can get the item and update it.

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

Looks like you are using your entity model as the parameter to update the entity values. A better approach to prevent over posting is to use a view model.

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • @ Shyju: I want it to update only three property names on purpose. My question is why its not updating these and returning back the view to all. – moka Dec 31 '16 at 23:34
  • are you getting any errors ? Did you check the table data to confirm it is not updating ? Your code looks fine (as long as you include the Id). – Shyju Jan 01 '17 at 00:59
  • nothing on db, I was hoping its updating and just not returning view, I am rewriting to remove async and see what that gives – moka Jan 01 '17 at 01:13