Sun*_* jp 5 c# asp.net-mvc linq-to-sql
我有一个这样的更新功能:
public void Update(HomeBanner homebanner)
{
homebanner.EnsureValid();
DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, homebanner);
DataSource.DataContext.SubmitChanges();
}
Run Code Online (Sandbox Code Playgroud)
我写了一个更新控制器
[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");
}
Run Code Online (Sandbox Code Playgroud)
然后是视图:
<% 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>
<% } %>
Run Code Online (Sandbox Code Playgroud)
它也获取数据但更新步骤不成功:它标记在这里
DataSource.DataContext.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, homebanner);
Run Code Online (Sandbox Code Playgroud)
并抛出异常:无法识别为刷新指定的对象。 我不知道为什么,我在调试时看到数据填充到对象。请有人帮助我!
检查那里的 DataContext 实例,也许您正在使用原始对象不存在的不同实例。
如果不存在,则必须首先将对象附加到数据上下文,然后调用刷新。
PS提示:制作用于与数据交互的模型或服务,在控制器中看起来很混乱;)'
| 归档时间: |
|
| 查看次数: |
2911 次 |
| 最近记录: |