在我的代码中,更新既发生在视图中,也发生在控制器中。创建和更新的相同控制器和视图。创建配置文件工作正常但在更新时显示DbConcurrency异常消息。请帮我找。
更新记录时出现异常消息: 存储更新,插入或删除语句会影响意外的行数(0)。自实体加载后,实体可能已被修改或删除。刷新ObjectStateManager条目。
我的更新个人资料页面有(Profile.cshtml)
@model Sitecss.Models.Profile
@using Microsoft.Web.Helpers;
@{
ViewBag.Title = "CreateProfile";
Layout = "~/Views/Shared/_HomeLay.cshtml";
}
@{
var GT = new SelectList(new[] {
new {ID ="Team DeathMatch", Name="Team DeathMatch"},
new {ID ="Search & Destroy", Name="Search & Destroy"},
new {ID ="Flag Runner", Name="Flag Runner"},
new {ID ="Domination", Name="Domination"},
new {ID ="Kill Confirmed", Name="Kill Confirmed"}
},"ID","Name");
var Spec = new SelectList(new []{
new {ID ="Assault", Name="Assault"},
new {ID ="Tactical", Name="Tactical"},
new {ID ="Long-Range Eliminations", Name="Long-Range Eliminations"},
new {ID ="Noob", Name="Noob"}
}, "ID", "Name");
}
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("CreateProfile", "Home", FormMethod.Post, new { @encType = "multipart/form-data" }))
{
<div id="content1">
@Html.ValidationSummary(true)
<h4>Create a Profile</h4>
<div>
@Html.LabelFor(m => m.SteamName)
</div>
<div>
@Html.TextBoxFor(m => m.SteamName, new { @class = "wide" })
@Html.ValidationMessageFor(m => m.SteamName)
</div>
<div>
@Html.LabelFor(m =>m.UserImg)
</div>
<div>
<input type="file" name="image" class="image" /> <br />
</div>
<div>
@Html.LabelFor(m => m.GameType)
</div>
<div>
@Html.DropDownListFor(m => m.GameType, GT, new { @class = "wide" })
@Html.ValidationMessageFor(m => m.GameType)
</div>
<div>
@Html.LabelFor(m => m.Specialist)
</div>
<div>
@Html.DropDownListFor(m => m.Specialist, Spec, new { @class = "wide" })
@Html.ValidationMessageFor(m => m.Specialist)
</div>
<div>
@Html.LabelFor(m => m.FavGun)
</div>
<div>
@Html.TextBoxFor(m => m.FavGun, new { @class = "wide" })
@Html.ValidationMessageFor(m => m.FavGun)
</div>
<p><input type="submit" class="button" value="Ok" /></p>
</div>
}
我的控制器功能
public ActionResult CreateProfile()
{
if (db.Profiles.Any(u => u.Username == User.Identity.Name))
{
Profile pro = (from usr in db.Profiles where usr.Username == User.Identity.Name select usr).Single();
olf = pro.UserImg;
Profile p = db.Profiles.Find(pro.Id);
ViewBag.ProfilePic = olf;
return View(p);
}
else
{
ViewBag.Name = User.Identity.Name;
return View();
}
}
[HttpPost]
public ActionResult CreateProfile(Profile m)
{
WebImage photo = WebImage.GetImageFromRequest();
string newFileName = "";
string thumbs = "";
if (photo != null)
{
string ext = Path.GetExtension(photo.FileName);
newFileName = User.Identity.Name+ ext;
thumbs = @"Images/" + newFileName;
photo.Resize(width: 120, height: 120, preserveAspectRatio: true, preventEnlarge: true);
photo.Save(@"~/"+thumbs);
m.UserImg = newFileName;
}
if (ModelState.IsValid)
{
if (db.Profiles.Any(u => u.Username == User.Identity.Name))
{
db.Entry(m).State = System.Data.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
m.Username = User.Identity.Name;
db.Profiles.Add(m);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(m);
}
答案 0 :(得分:0)
在我的编辑控制器中向更新语句添加断点时,我发现更新时主键ID = 0以解决此问题我已在更新中修复了DbConcurrency的视图中添加了隐藏字段