Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An object specified for refresh is not recognized

I have a update function like this:

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

And i write a 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 the 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 get data as well but update step is not success: It mark right here

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

and throw exception: An object specified for refresh is not recognized. I dont know why, i see data filled to object when i debug. Plz someone help me!

like image 428
Sunshine jp Avatar asked Nov 04 '22 07:11

Sunshine jp


1 Answers

Check the instance of DataContext there, maybe you are using different instance in which original object doesn't exists.

If it doesn't exist, you must first attach object to data context, then call refresh.

P.S. a tip: Make model or service for interacting with data, in controller it looks messy ;)'

like image 80
MiBu Avatar answered Nov 09 '22 03:11

MiBu