微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有这样的更新功能

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.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐