The object specified for the update is not recognized

I have an update function, for example:

public void Update(HomeBanner homebanner) { homebanner.EnsureValid(); DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, homebanner); DataSource.DataContext.SubmitChanges(); } 

And I am writing an update controller

 [AcceptVerbs(HttpVerbs.Post)] //[ValidateAntiForgeryToken] [ValidateInput(false)] public ActionResult ManageImages(int ? id,FormCollection form) { HomeBanner homebanner= BannerRepository.RetrieveById(id); this.TryUpdateModel(homebanner); string photoName = saveImage("photo"); if (photoName != string.Empty) homebanner.ImageID = photoName; BannerRepository.Update(homebanner); return RedirectToAction("list", "Admin"); } 

and then view:

 <% using (Html.BeginForm("ManageImages", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })) {%> <h3>Manage Images</h3> <div class="label-field"> <label for="ID">Chọn vị trí:</label> <%= Html.DropDownList("ID", DataHelper.Banner().ToList().ToSelectList("value", "name",Model.HomeBanner.ID.ToString()))%> </div> <div class="label-field"> <label for="photo"> Chọn hình</label> <input type="file" name="photo" value=""/> </div> <div class="label-field"> <label for="Link"> Liên kết</label> <input type="text" name="Link"/> </div> <p> <input type="submit" value="Lưu" /> </p> <% } %> 

It also receives data, but the update step was unsuccessful: check here

 DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, homebanner); 

and throw exception: The object specified for the update is not recognized. I don’t know why, I see the data populated by the object when I debug it. Plz someone helping me!

+4
source share
1 answer

Check the instance of the DataContext there, maybe you are using another instance in which the source object does not exist.

If it does not exist, you must first bind the object to the data context, and then invoke the update.

PS tip: make a model or service for interacting with data, in the controller it looks messy;) '

+1
source

Source: https://habr.com/ru/post/1391793/


All Articles