更新数据库中的一个属性 class
Update One attribute in database class
我的代码有问题,我尝试使用 EF Code First 从数据库更新一个字段。我在其他项目中使用了相同的代码,我记得它工作正常,但我不知道为什么它现在不工作了。
这是我的观点:
<p id="myElem" class="alert-danger" style="display:none"></p>
<button class="downcount btn btn-warning mt-15 btn-block" id="downcount" data-id="@Model.Post.Id"><i class="fa fa-cloud-download">Click Here</i></button>
<script type="text/javascript">
$('.downcount').click(function () {
var myId = $(this).data('id');
$.ajax({
type: "GET",
url: '@Url.Action("DownloadNum", "Course")?Post_id=' + myId,
success: function (response) {
$('#myElem').html(response).fadeIn('slow');
$('#myElem').delay(8000).fadeOut('slow');
},
failure: function (response) {
alert("Failure");
}
});
});
</script>
这是我的控制器:
[HttpGet]
public ActionResult DownloadNum(int Post_id)
{
var result = db.Post.Where(p => p.Id == Post_id).FirstOrDefault();
if (result != null)
{
result.DownloadCount = result.DownloadCount+1;
db.SaveChanges();
return Content("Download Started !");
}
else
{
return Content("File is not on the server or deleted, Please report us to renew it!");
}
}
我尝试调试和跟踪过程,没有问题或错误,只是在数据库保存并检查数据库后没有更新或更改!
这是因为您没有在数据库中添加、删除或修改任何内容。我假设您想 modify/edit 因为您正在通过这行代码搜索记录:
var result = db.Post.Where(p => p.Id == Post_id).FirstOrDefault();
// Id is a UNIQUE property, so technically you should be using .Single()
// or .SingleOrDefault() because there should only be ONE record where p.Id == Post_id
// or you could use:
// var result = db.Post.Find(Post_id);
那么,你想修改那个结果。
result.DownloadCount = result.DownloadCount+1;
db.Entry(result).EntityState = EntityState.Modified; // missing line
db.SaveChanges();
作为旁注,在您的 jQuery.. 如果您的视图使用模型.. 您可以更改此行:
url: '@Url.Action("DownloadNum", "Course")?Post_id=' + myId,
收件人:
url: '@Url.Action("DownloadNum", "Course", new { Post_id = Model.Id })',
@Url.Action
已超载,您可以在其中合并路由值。
如果有帮助请告诉我!
我的代码有问题,我尝试使用 EF Code First 从数据库更新一个字段。我在其他项目中使用了相同的代码,我记得它工作正常,但我不知道为什么它现在不工作了。
这是我的观点:
<p id="myElem" class="alert-danger" style="display:none"></p>
<button class="downcount btn btn-warning mt-15 btn-block" id="downcount" data-id="@Model.Post.Id"><i class="fa fa-cloud-download">Click Here</i></button>
<script type="text/javascript">
$('.downcount').click(function () {
var myId = $(this).data('id');
$.ajax({
type: "GET",
url: '@Url.Action("DownloadNum", "Course")?Post_id=' + myId,
success: function (response) {
$('#myElem').html(response).fadeIn('slow');
$('#myElem').delay(8000).fadeOut('slow');
},
failure: function (response) {
alert("Failure");
}
});
});
</script>
这是我的控制器:
[HttpGet]
public ActionResult DownloadNum(int Post_id)
{
var result = db.Post.Where(p => p.Id == Post_id).FirstOrDefault();
if (result != null)
{
result.DownloadCount = result.DownloadCount+1;
db.SaveChanges();
return Content("Download Started !");
}
else
{
return Content("File is not on the server or deleted, Please report us to renew it!");
}
}
我尝试调试和跟踪过程,没有问题或错误,只是在数据库保存并检查数据库后没有更新或更改!
这是因为您没有在数据库中添加、删除或修改任何内容。我假设您想 modify/edit 因为您正在通过这行代码搜索记录:
var result = db.Post.Where(p => p.Id == Post_id).FirstOrDefault();
// Id is a UNIQUE property, so technically you should be using .Single()
// or .SingleOrDefault() because there should only be ONE record where p.Id == Post_id
// or you could use:
// var result = db.Post.Find(Post_id);
那么,你想修改那个结果。
result.DownloadCount = result.DownloadCount+1;
db.Entry(result).EntityState = EntityState.Modified; // missing line
db.SaveChanges();
作为旁注,在您的 jQuery.. 如果您的视图使用模型.. 您可以更改此行:
url: '@Url.Action("DownloadNum", "Course")?Post_id=' + myId,
收件人:
url: '@Url.Action("DownloadNum", "Course", new { Post_id = Model.Id })',
@Url.Action
已超载,您可以在其中合并路由值。
如果有帮助请告诉我!