For some reason database will not update a post, even though it navigates to right page which suggests that it should have saved the changes. Here's my code below. Note: I tried this with EF, now using repository pattern and still the same issue.
public class PostsController : Controller
{
public IRepository _repo;
public PostsController(IRepository repo)
{
_repo = repo;
}
public IActionResult Index()
{
return View();
}
public IActionResult Post()
{
return View();
}
[HttpGet]
public IActionResult Edit()
{
return View(new Post());
}
[HttpPost]
public async Task<IActionResult> Edit(Post post)
{
_repo.AddPost(post);
if (await _repo.SaveChangesAsync())
return RedirectToAction("Index");
else
return View(post);
}
}
Once I create a post, the page navigates to Index which is suggesting that SaveChanges();
is working without exceptions. However if I view my Db, it's not there!
Here's my db context just inc. :
public class ApplicationDbContext :
IdentityDbContext<ApplicationUser,
ApplicationRole,
string,
IdentityUserClaim<string>,
IdentityUserRole<string>,
IdentityUserLogin<string>,
IdentityRoleClaim<string>,
IdentityUserToken<string>>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Post> Posts { get; set; }
}
And here's my repository clearly implementing db context:
private ApplicationDbContext _context;
public Repository(ApplicationDbContext context)
{
_context = context;
}
Here's my repository and irepository code as well as my configure services from startup.cs.:
Repository.cs:
{
public class Repository : IRepository
{
private ApplicationDbContext _context;
public Repository(ApplicationDbContext context)
{
_context = context;
}
public void AddPost(Post post)
{
_context.Posts.Add(post);
}
public List<Post> GetAllPosts(int id)
{
return _context.Posts.ToList();
}
public Post GetPost(int id)
{
return _context.Posts.FirstOrDefault(p => p.Id == id);
}
public void RemovePost(int id)
{
_context.Posts.Remove(GetPost(id));
}
public void UpdatePost(Post post)
{
_context.Posts.Update(post);
}
public async Task<bool> SaveChangesAsync()
{
if (await _context.SaveChangesAsync() > 0)
{
return true;
}
return false;
}
}
}
Here's my irepository.cs
{
public interface IRepository
{
Post GetPost(int id);
List<Post> GetAllPosts(int id);
void AddPost(Post post);
void UpdatePost(Post post);
void RemovePost(int id);
Task<bool> SaveChangesAsync();
}
}
Here's my configure services code
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddTransient<IRepository, Repository>();
services.AddMvc(config =>
{
// using Microsoft.AspNetCore.Mvc.Authorization;
// using Microsoft.AspNetCore.Authorization;
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
My post.cs entity is here:
{
public class Post
{
public int Id { get; set; }
[Required(ErrorMessage = "Title is required")]
public virtual string Title { get; set; } = "";
public virtual string Short_Description { get; set; }
public string Image { get; set; } = "";
[DataType(DataType.MultilineText)]
public virtual string Content { get; set; }
public DateTime Posted { get; set; } = DateTime.Now;
public DateTime? Modified { get; set; } = DateTime.Now;
}
}
Comments
Post a Comment