asp core- crash course take away
model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace PokharaVoyage_webapp.Models
{
public class GetJoke
{
public int Id { get; set; }
public string jokequestion { get; set; }
public int jokeanswer { get; set; }
public GetJoke()
{
}
}
}
2. creating a controller with view and entity.. selecting applicationdb context and above model
automatically creates create delete update and detail view
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using PokharaVoyage_webapp.Models;
using WebApplication.Data;
namespace WebApplication.Controllers
{
public class GetJokesController : Controller
{
private readonly ApplicationDbContext _context;
public GetJokesController(ApplicationDbContext context)
{
_context = context;
}
// GET: GetJokes
[Authorize]
public async Task<IActionResult> Index()
{
return View(await _context.GetJoke.ToListAsync()); //its a database table name
}
// GET: SearchJokes
public async Task<IActionResult> SearchJokes()
{
return View(); //its a database table name
}
public async Task<IActionResult> SearchJokesResult(string SearchBatallion) // should be the same name as input type
{
return View( "Index", await _context.GetJoke.Where ( j => j.jokequestion.Contains(SearchBatallion)).ToListAsync());
// return "you entered"+ SearchBatallion; //its a database table name
}
// GET: GetJokes/Details/5
[Authorize]
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var getJoke = await _context.GetJoke
.FirstOrDefaultAsync(m => m.Id == id);
if (getJoke == null)
{
return NotFound();
}
return View(getJoke);
}
// GET: GetJokes/Create
[Authorize]
public IActionResult Create()
{
return View();
}
// POST: GetJokes/Create
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,jokequestion,jokeanswer")] GetJoke getJoke)
{
if (ModelState.IsValid)
{
_context.Add(getJoke);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(getJoke);
}
// GET: GetJokes/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var getJoke = await _context.GetJoke.FindAsync(id);
if (getJoke == null)
{
return NotFound();
}
return View(getJoke);
}
// POST: GetJokes/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,jokequestion,jokeanswer")] GetJoke getJoke)
{
if (id != getJoke.Id)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(getJoke);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!GetJokeExists(getJoke.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(getJoke);
}
// GET: GetJokes/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var getJoke = await _context.GetJoke
.FirstOrDefaultAsync(m => m.Id == id);
if (getJoke == null)
{
return NotFound();
}
return View(getJoke);
}
// POST: GetJokes/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var getJoke = await _context.GetJoke.FindAsync(id);
_context.GetJoke.Remove(getJoke);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool GetJokeExists(int id)
{
return _context.GetJoke.Any(e => e.Id == id);
}
}
}
3. For migration script see note copy.
4.[Authorization] decorator for session control
microsoft.asp.dotnet.authorization should be imported
Comments
Post a Comment