Giter Site home page Giter Site logo

cheesemvc-1's Introduction

CheeseMVC

Class 13 Prep

Class 13 Prep

NOTE: Again, I augment our existing CheeseMVC app, and do not follow the starter code exactly. See my comments for Class 12 Prep below.

Class 12 Prep

Class 12 Prep

NOTE: The code for class 12 prep does not follow the video tutorials exactly. It is not based on the Entity Framework starter project, but is rather a natural progression from previous CheeseMVC lessons. The chief differences are:

  • there is no CategoryController, all category routes are in CheeseController
  • there is no user interface for creating a new CheeseCategory, this must be done manually through SQL Server Object Explorer
  • The view models may contain properties that are named differently (for example, NewCheeseViewModel.SelectedCheeseCategoryID instead of NewCheeseViewModel.CategoryID)
  • there are several more migrations because I changed the Cheese class incrementally

Class 9

Class 9 Prep

NOTE: I did not start/end with the Git branches specified in the prep work description. All of my code modifications may be found in the class-9-prep branch.

Class 8

Class 8 Prep

NOTE: I did not start/end with the Git branches specified in the prep work description. All of my code modifications may be found in the class-8-prep branch.

Class 5

Class 5 Prep

Class 4

Class 4 Prep

Intro to ASP.NET Core MVC: Views (Part 1)

  • controller => C# class
  • view => .cshtml file

By default, "views" (.cshtml files) are located:

  • PROJECT/Views/[ControllerName]/[ActionName].cshtml
  • PROJECT/Views/Shared/[ActionName].cshtml

Razor templates (.cshtml) are similar to Jinja templates in Python in that they can contain both HTML markup and view logic.

ViewData is a specizlized Dictionary that acts as a "bucket" of data passed into the view.

To share a view across different controllers, put it in /Views/Shared.

C# code can be added to a view by prefixing it with an @ symbol.

A default layout can be applied to all views by adding the file: /Views/Shared/_Layout.cshtml.

Changes to views do not require a server restart ("hot deployment").

Intro to ASP.NET Core MVC: Views (Part 2)

Code blocks can be added to Razor views by preceeding them with the @ symbol:

<ul>
    @foreach (string cheese in cheeses)
    {
        <li>@cheese</li>
    }
</ul>

It is generally not a good idea to create data in view files!

There are multiple ways to pass data to a view.

ViewBag is a "dynamic" object that can be assigned properties of any type that contain data for the view. This is very similar to, but not the same, as ViewData. (See: ViewModel vs ViewData vs ViewBag vs TempData vs Session in MVC)

Links (<a> tags) can use asp-* attributes to generate href values:

<a asp-controller="Cheese" asp-action="Add">Add Cheese</a>

Static class properties can make data available to any instance of a class:

public class CheeseController : Controller
{
    // available to *all* instances of CheeseController;
    // all instances *share* this list
    private static List<string> cheeses = new List<string>();

    // created per-instance; each instance gets its own list
    private List<string> moreCheeses = new List<string>();
}

The Controller.Redirect() method returns a RedirectResult, which can be returned from a controller action method to take the user to some other route.

Studio

Routing parameters can be added to anchors using special MVC helper attributes.

In the controller:

[Route("/cheese/remove/{cheeseName}")]
[HttpGet]
public IActionResult RemoveSingleCheese(string cheeseName)
{
    // ... remove the cheese
    return Redirect("/cheese");
}

In the view:

<a asp-controller="Cheese"
   asp-action="RemoveSingleCheese"
   asp-route-cheeseName="@cheeseName">delete</a>

Inputs can be submitted as an array of values to an action method.

In the controller:

[Route("/cheese/remove")]
[HttpPost]
public IActionResult RemoveManyCheeses(string[] selectedCheeses)
{
    // ... remove the cheeses
    return Redirect("/cheese");
}

In the view:

<form method="post">
<ul>
    @foreach (KeyValuePair<string, string> cheese in ViewBag.cheeses)
    {
        <li>
            <input name="selectedCheeses[]" type="checkbox" value="@cheese.Key" />
            <label>@cheese.Key</label>
        </li>
    }
</ul>
<input type="submit" valu="Delete Cheeses"/>
</form>

cheesemvc-1's People

Contributors

nicholascloud avatar

Watchers

James Cloos 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.