加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 无法识别为刷新指定的对象

发布时间:2020-12-15 21:29:35 所属栏目:百科 来源:网络整理
导读:我有这样的更新功能: public void Update(HomeBanner homebanner) { homebanner.EnsureValid(); DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues,homebanner); DataSource.DataContext.SubmitChanges(); } 我写了一个更新
我有这样的更新功能:

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

我写了一个更新控制器

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

    }

然后是观点:

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

它也获取数据,但更新步骤不成功:它标记在这里

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

并抛出异常:无法识别为刷新指定的对象.
我不知道为什么,我在调试时看到数据填充到对象. Plz有人帮帮我!

解决方法

检查那里的DataContext实例,也许你正在使用不存在原始对象的不同实例.

如果它不存在,则必须先将对象附加到数据上下文,然后调用refresh.

附:提示:制作模型或服务以与数据交互,在控制器中它看起来很混乱;)’

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读