c# - 如何让用户在.Net Core中只喜欢/不喜欢图片一次
问题描述
我想让用户每张图片只能给一个喜欢或不喜欢一次。我做了一个额外的模型
public class Marking
{
public int IdMema { get; set; }
public string Authorr {get;set;}
public int Like { get; set; }
public int Dislike { get; set; }
}
这在 Memy 模型中
public partial class Memy
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[HiddenInput]
public int Id_mema { get; set; }
public string Autor { get; set; }
[Required]
public string Title { get; set; }
[HiddenInput]
public string coverImg { get; set; }
public string Description { get; set; }
public string Category { get; set; }
[HiddenInput]
public DateTime? releaseDate { get; set; }
public DateTime? modifyDate { get; set; }
public int? Like { get; set; }
public int? Dislike { get; set; }
}
这是 MemyController 中的方法
public async Task<IActionResult> Like(int id, Marking user)
{
System.Security.Claims.ClaimsPrincipal currentUser = this.User;
var memy = db.Memy.SingleOrDefault(s => s.Id_mema == id);
memy.Like++;
List<Marking> ee = new List<Marking>();
if (id != memy.Id_mema)
{
return NotFound();
}
foreach (var z in ee)
{
if (ModelState.IsValid && z.Authorr == currentUser.Identity.Name && z.IdMema == id && z.Like == 0)
{
z.Like = 1;
db.Update(memy);
await db.SaveChangesAsync();
}
return RedirectToAction("Show", new { id = id });
}
return RedirectToAction("Show", new { id = id });
}
我不知道为什么这不起作用。我在这里放了一个断点:
if (ModelState.IsValid && z.Authorr == currentUser.Identity.Name && z.IdMema == id && z.Like == 0)
并且程序没有检查if,为什么?我在 MemyController 中更改了我的方法
public async Task<IActionResult> Like(int id)
{
var memy = db.Memy.SingleOrDefault(s => s.Id_mema == id);
var marking = db.Marking.SingleOrDefault(s => s.IdMema == id);
memy.Like++;
if (id != memy.Id_mema)
{
return NotFound();
}
System.Security.Claims.ClaimsPrincipal currentUser = this.User;
if (ModelState.IsValid && marking.IdMema==id && marking.Authorr==currentUser.Identity.Name && marking.CountLike==0)
{
db.Update(memy);
await db.SaveChangesAsync();
return RedirectToAction("Show", new { id = id });
}
return RedirectToAction("Show", new { id = id });
}
``
and I add-migration and update-database ,but every time I receive answer that datebase do not contain Marking
Thanks for help :)
解决方案
推荐阅读
- regex - 使用多格式解析器 (Regex) 在 FluentD 中解析 Nginx 入口访问日志
- python - 正则表达式模式的新手。如何捕获多行?
- r - Gggplot grid.arrange all in one row 看起来像一个无缝的情节
- node.js - 使用 bcrypt 散列密码时出错:找不到模块 bcrypt_lib.node
- pepper - 编排中的时间线回到原始位置
- python - 创建一个 Python 函数,将一列的值替换为另一列的值
- c - C语言函数的内存地址
- javascript - JS:将一个对象解构为另一个具有重命名属性的对象
- vue.js - Vuex 存储更改在组件中没有反应
- multithreading - 如何在 Rust 中使用多个线程在结构方法中处理向量?