Giter VIP home page Giter VIP logo

asp_net_mvc's Introduction

ASP_NET_MVC

How to set up a website using asp.net and enitity framework!

Steps

    1. Download Visual Studio 2017+
    1. Install Asp.net
    1. If you have issues with the installation it might be due to the fact that your NuGet settings are Offline.
    • Package Manager Settings
    • in that case add this source and it should work fine
  • Next to use this there are 3 main components needed to work with.
  • Solutions Explorer
    • M: Model
    • V: View
    • C: Controller
    • MVC view
    • Basically to get them to interact we need to establish an understanding and the above image shows the basic principle of how the data will flow from start to finish.

In most cases I think starting with the model will be the most simple due to it being kinda of a core to the program you want to build. I.e. lets build a website to show jokes. I can then make a model that would contain properties about what I want for the site.

jokeclass

so my id would be later linked to a db that would have the id of the joke stored, the joke and the answer along with the user who posted it.

After making the class you can then make a controller to manage the data. ---> I would recommend using the automatic generation features to make all the code for you other wise self coding can take a bit and be annoying.

After generating the code you should have a Controller like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using JokesWebApp_MVC.Data;
using JokesWebApp_MVC.Models;
using Microsoft.AspNetCore.Authorization;

namespace JokesWebApp_MVC.Controllers
{
    public class JokesController : Controller
    {
        private readonly ApplicationDbContext _context;

        public JokesController(ApplicationDbContext context)
        {
            _context = context;
        }

        // GET: Jokes
        public async Task<IActionResult> Index()
        {
            return View(await _context.Joke.ToListAsync());
        }

        // POST: Jokes/ShowSearchResults
        public async Task<IActionResult> ShowSearchForm()
        {
            return View();
        }

        public async Task<IActionResult> ShowSearchResults(String SearchPhrase)
        {
            return View("Index",await _context.Joke.Where(j => j.JokeQuestion.Contains(SearchPhrase)).ToListAsync());
        }


        // GET: Jokes/Details/5
        [Authorize]
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var joke = await _context.Joke
                .FirstOrDefaultAsync(m => m.Id == id);
            if (joke == null)
            {
                return NotFound();
            }

            return View(joke);
        }

        // GET: Jokes/Create
        [Authorize]
        public IActionResult Create()
        {
            return View();
        }

        // POST: Jokes/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.
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,JokeQuestion,JokeAnswer,User")] Joke joke)
        {
            if (ModelState.IsValid)
            {
                _context.Add(joke);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(joke);
        }

        // GET: Jokes/Edit/5
        [Authorize]
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var joke = await _context.Joke.FindAsync(id);
            if (joke == null)
            {
                return NotFound();
            }
            return View(joke);
        }

        // POST: Jokes/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.
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Id,JokeQuestion,JokeAnswer,User")] Joke joke)
        {
            if (id != joke.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(joke);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!JokeExists(joke.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(joke);
        }

        // GET: Jokes/Delete/5
        [Authorize]
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var joke = await _context.Joke
                .FirstOrDefaultAsync(m => m.Id == id);
            if (joke == null)
            {
                return NotFound();
            }

            return View(joke);
        }

        // POST: Jokes/Delete/5
        [Authorize]
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var joke = await _context.Joke.FindAsync(id);
            _context.Joke.Remove(joke);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool JokeExists(int id)
        {
            return _context.Joke.Any(e => e.Id == id);
        }
    }
}

This will generate along side the Views which should create a Create, Delete, Details, Edit, and Index view and all should be linked up more or less.

Now if you run the page it should look somthing along the lines of this. I added some images so dont worry. testwebpage

  • The only issue might occur, i cant remember if this happens b4 or after you click on the jokes page that i have but you can access it in the url via /jokes if you set up the site properly. --- You should get an error due to not linking your db with the site. That is an ez fix. In this case we will use migrations.

  • ^^ this is cuz no one likes using DAO ( data access object) but we rather use ORM or (OBject relational Mapper) basically get vs to do the work for us rather than writing our own sql.

you are gonna want to type few lines in the package manager console.

enable-migrations
add-migration "What ever you wanna write here"
update-database

After this the website should be up and you can add or remove and play around with it anyway you want!

We have search feature. search

Screenshot 2021-05-28 191929

We have login page. login


What is dependency Injection? what is dependency

So as an example What we want is to inject the repo dependency into the item controller

what we want what we get

So to register and construct the dependencies Depend

How to register and inject dependencies in .NET 5

How to implement Data Transfer Objects (DTOs)

How to map entities to DTOs

asp_net_mvc's People

Contributors

capactiyvirus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.