mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
56 lines
1.5 KiB
C#
56 lines
1.5 KiB
C#
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);
|
|
}
|
|
}
|
|
} |