Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Level 1 Section 1
The Problem
Were playing a tabletop game and keeping game information written on notebook paper,
which is getting troublesome.
Hard to search
Updating/erasing is messy
Models
Views
Controllers
Properties
Models
Home.cs
Views
Home
Index.cshtml
Visual Studio
Visual Studio Code
Xamarin Studio
MonoDevelop
SharpDevelop
<h2>Characters:</h2>
<div>
<ul>
<li>Hux</li>
<li>Jasmine</li>
<li>Robert</li>
<li>Thomas</li>
</ul>
</div>
CSHTML
<h2>Characters:</h2>
<div>
<ul>
<li>Jasmine</li>
</ul>
</div>
CSHTML
CSHTML
<div>
<ul>
<li>Jasmine</li>
</ul>
</div>
@model String
<h2>Characters:</h2>
<div>
<ul>
<li>@Model</li>
</ul>
</div>
CSHTML
CSHTML
Uppercase
<div>
<ul>
<li>@Model</li>
</ul>
</div>
Models
Views
Home
Index.cshtml
Namespace
Class
Method
CS
namespace CharacterSheetApp.Controllers
{/
public class HomeController : Controller
{.
public IActionResult Index()
{)
return View();
}?
}+
}'
CS
namespace CharacterSheetApp.Controllers
{/
public class HomeController : Controller
{.
We create the string
public IActionResult Index()
return to our view
{)
var name = "Hux";
return View("Index",name);
}?
}+
}'
we want to
Level 1 Section 2
Controller
./Controllers/HomeController.cs
Action
CS
namespace CharacterSheetApp.Controllers
{.
public class HomeController : Controller
{,
public IActionResult Index()
{'
return View();
};
}}\
CS
Views
Home
Index.cshtml
http://www.example.com
http://www.example.com/Home/Index
Level 1 Section 3
Models
Character.cs
Views
Home
Index.cshtml
namespace CharacterSheetApp.Models
{
public class Character
{
public string Name;
}
}
cs
CS
Character model.
HomeController.cs before
public IActionResult Index()
{
var name = new string();
name = "Hux";
return View("Index",name);
}
Namespaces in .NET
.NET really pushes division of concerns. One example is that namespaces often follow
directories and keep us from accessing other parts of the code that we dont intend to.
CharacterSheetApp
Namespace
Models
CharacterSheetApp.Models
Views
CharacterSheetApp.Views
Controllers
CharacterSheetApp.Controllers
Properties
CS
namespace CharacterSheetApp.Models
{
Namespace
public class Character
{
Class
CS
CSHTML
@model CharacterSheetApp.Models.Character
<h2>Characters:</h2>
<div>
<ul>
<li>@Model</li>
</ul>
</div>
CSHTML
@model CharacterSheetApp.Models.Character
<h2>Characters:</h2>
<div>
<ul>
<li>@Model.Name</li>
</ul>
</div>
Level 2
Level 2 Section 1
The Problem
In the last level, we set up our view to show our names
dynamically, but then we hard-coded that name in the
controller. Instead, we need to use user input.
Steps to the Solution
1. Create form in view
2. Add Create() method to controller
3. Move existing logic out of Index() into Create()
CSHTML
@model CharacterSheetApp.Models.Character
<h2>Characters:</h2>
<form>
view for a simple form.
<div>
<input name="CharacterName" />
<input type="submit" value="Add Character" />
</div>
</form>
CSHTML
@model CharacterSheetApp.Models.Character
<h2>Characters:</h2>
will match
CS
CS
CS
CS
CS
@model CharacterSheetApp.Models.Character
<h2>Characters:</h2>
asp-action
CSHTML
we
CSHTML
@model CharacterSheetApp.Models.Character
controller
<h2>Characters:</h2>
Level 3 Section 1
Retaining Data
Remembering Our Input
The Problem
In our last level, we got everything set up so our users could enter character names, but its
not working quite as expected
Resizing an array is
a lot of code.
Resizing a list is a
CSHTML
@model List<CharacterSheetApp.Models.Character>
<h2>Characters:</h2>
<form asp-action=Create>
we need to change it
<div>
<input name=CharacterName />
<input type=submit value=Add Character />
</div>
</form>
into a collection.
CSHTML
@model List<CharacterSheetApp.Models.Character>
<h2>Characters:</h2>
<form></form>
<div>
<ul>
<li>@Model.Name</li>
</ul>
</div>
CSHTML
<ul>
@foreach (var item in Model)
{-
C#
<li>
<label>@item.Name</label>
</li>
HTML
}+
</ul>
CSHTML
Were using global variables here since were not ready to jump into
databases yet. That being said, use global variables sparingly as
CS
New class
namespace CharacterSheetApp
{=
public class GlobalVariables
{_
public List<CharacterSheetApp.Models.Character> Characters;
}\
A variable that holds
}[
list of characters
CS
namespace CharacterSheetApp
{=
public static class GlobalVariables
{_
public static List<CharacterSheetApp.Models.Character> Characters;
}\
}[
We only want one character list, so it and the
CS
namespace CharacterSheetApp
{=
public static class GlobalVariables
{_
public static List<CharacterSheetApp.Models.Character> Characters;
}\
}[
CS
namespace CharacterSheetApp
{=
public static class GlobalVariables
{_
public static System.Collections.Generic. List
<CharacterSheetApp.Models.Character> Characters;
}\
}[
using System.Collections.Generic;
CS
namespace CharacterSheetApp.Models
{
public class Character
{
public string Name;
}.
},
CS
CS
List collections have an add() method you can use to add objects
to the list.
error, so check if its null and initialize it in the event that it is.
CS
CS
C#
CS
CS
CS
CS
CS
C#
CS
using System.Collections.Generic;
namespace CharacterSheetApp
{-
public static class GlobalVariables
{=
public static List<Character> Characters { get; set; }
};
}'
namespace CharacterSheetApp
{-
public static class GlobalVariables
{=
public static List<Character> Characters { get; set; }
= new List<Character>();
};
}'
CS