-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathBlogsController.cs
More file actions
56 lines (47 loc) · 1.47 KB
/
BlogsController.cs
File metadata and controls
56 lines (47 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using EFGetStarted.AspNetCore.NewDb.Models;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace EFGetStarted.AspNetCore.NewDb.Controllers
{
public class BlogsController : Controller
{
private BloggingContext _context;
public BlogsController(BloggingContext context)
{
_context = context;
}
public IActionResult Index()
{
return View(_context.Blogs.ToList());
}
public IActionResult Search(string Owner)
{
// Option 1: .Net side filter using LINQ:
var blogs = _context.Blogs
.Where(b => b.Owner.Name == Owner)
.ToList();
// Option 2: SQL Server filter using T-SQL:
//var blogs = _context.Blogs
// .FromSql<Blog>(@"SELECT * FROM Blogs
// WHERE JSON_VALUE(Owner, '$.Name') = {0}", Owner)
// .ToList();
return View("Index", blogs);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(Blog blog)
{
if (ModelState.IsValid)
{
_context.Blogs.Add(blog);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(blog);
}
}
}