为刷新指定的对象不能被识别 [英] 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");
}
和则认为:
<% 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);
和抛出异常:作为刷新指定的对象无法识别
我不知道为什么,我看到装满时,我调试对象的数据。 PLZ有人能帮助我!
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!
推荐答案
检查DataContext的实例有,也许你正在使用它原来的对象不存在不同的实例。
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。一个提示:请模型或服务与数据交互,在控制器看起来凌乱;)
P.S. a tip: Make model or service for interacting with data, in controller it looks messy ;)'
这篇关于为刷新指定的对象不能被识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!