ASP.NET MVC Cheat Sheet, Schemes and Mind Maps of Computer Networks

Entity Framework / Validation Model namespace MyApp.Model { public class Employee. {. [Key]. [DatabaseGeneratedAttribute(. DatabaseGeneratedOption.

Typology: Schemes and Mind Maps

2021/2022

Uploaded on 09/07/2022

zaafir_ij
zaafir_ij 🇦🇪

4.4

(61)

884 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Entity Framework / Validation Model
namespace MyApp.Model {
public class Employee
{
[Key]
[DatabaseGeneratedAttribute(
DatabaseGeneratedOption.Identity)]
public int EmployeeId { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string FamilyName { get; set; }
[EmailAddress]
public string Email { get; set; }
[RegularExpression("\\d+\\.\\d+\\.\\d+\\.\\d+",
ErrorMessage = "Invalid IP address format")]
public string IpAddress { get; set; }
public virtual Department Department { get; set; }
[InverseProperty("Author")]
public virtual ICollection<Message> Messages { get; set; }
public bool IsFullTime { get; set; }
[Range(1, 12)]
public int BirthMonth { get; set; }
[Timestamp]
public byte[] RowVersion { get; set; }
[NotMapped]
public string FullName
{
get { return FirstName + " " + FamilyName; }
}
}
}
Property value must match the
supplied regular expression
This list contains all Messages with an
Author property that is this Employee
Property value must be between 1
and 12 (inclusive)
This property is to be ignored by
Entity Framework
Database primary key
Key is automatically generated by
incrementing a counter
Property value cannot be null / empty
Property value must be formatted as
an email address
Declare navigation properties to be
virtual if you want lazy loading
This property is a timestamp used for
optimistic concurrency control
pf3
pf4
pf5

Partial preview of the text

Download ASP.NET MVC Cheat Sheet and more Schemes and Mind Maps Computer Networks in PDF only on Docsity!

Entity Framework / Validation Model

namespace MyApp.Model { public class Employee { [Key] [DatabaseGeneratedAttribute( DatabaseGeneratedOption.Identity)] public int EmployeeId { get; set; } [Required] public string FirstName { get; set; } [Required] public string FamilyName { get; set; } [EmailAddress] public string Email { get; set; } [RegularExpression("\d+\.\d+\.\d+\.\d+", ErrorMessage = "Invalid IP address format")] public string IpAddress { get; set; } public virtual Department Department { get; set; } [InverseProperty("Author")] public virtual ICollection Messages { get; set; } public bool IsFullTime { get; set; } [Range(1, 12)] public int BirthMonth { get; set; } [Timestamp] public byte[] RowVersion { get; set; } [NotMapped] public string FullName { get { return FirstName + " " + FamilyName; } } } }

Property value must match the

supplied regular expression

This list contains all Messages with an

Author property that is this Employee

Property value must be between 1

and 12 (inclusive)

This property is to be ignored by

Entity Framework

Database primary key

Key is automatically generated by

incrementing a counter

Property value cannot be null / empty

Property value must be formatted as

an email address

Declare navigation properties to be

virtual if you want lazy loading

This property is a timestamp used for

optimistic concurrency control

Razor View

@using MyApp.Models @model Employee @{ Layout = "~/Views/_Base.cshtml"; ViewBag.Title = "Employee Information"; }

Employee Information

These are the details of **@Model.FullName** :

**@using (Html.BeginForm())** { **@Html.AntiForgeryToken() @Html.ValidationSummary()**
First Name: **@Html.TextBoxFor(x => x.FirstName)**
Family Name: **@Html.TextBoxFor(x => x.FamilyName)**
Birth Month: **@Html.TextBoxFor(x => x.BirthMonth)**
IP Address: **@Html.TextBoxFor(x => x.IpAddress)**
Full Time: **@Html.DropDownListFor( x => x.IsFullTime, new [] { new SelectListItem { Text = "Full Time", Value = "true", Selected = Model.IsFullTime }, new SelectListItem { Text = "Part Time", Value = "false", Selected = !Model.IsFullTime } })**

Import namespace into Razor view

Declare a typed View

Use a master layout / template

Set a property of the ViewBag

dynamic object

Write out a value

Create a HTML
tag

Generate a token to prevent Cross-

Site-Forgery-Requests (CSFRs)

Show any validation errors

Create Text-boxes for Properties of

the Model

Create a Drop-down list for IsFullTime

property of the model

Controller

using System.Data.Entity; namespace MyApp.Controllers { [Authorize] public class EmployeeController : Controller { [AllowAnonymous] public ActionResult Index() { using (var context = new MyAppContext()) { var emps = context.Employees .Include(x => x.Messages).Include(x => x.Department) .ToList(); return View(emps); } } [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] [Authorize(Roles = "Administrator")] public ActionResult Delete(int id) { using (var context = new MyAppContext()) { var employee = new Employee { EmployeeId = id }; context.Employees.Attach(employee); context.Employees.Remove(employee); context.SaveChanges(); return RedirectToAction("Index"); } }

Contains the .Includes(…) extension

method used for Eager loading

A user must be logged in to use the

Actions on this controller

An exception to the need for

authorization (no need to login)

Eager loading for view rendering

(when there is no DbContext)

Accept HTTP verbs GET and POST

Require the user to belong to the

Administrator Role

Show the strongly typed View

matching this Action (Index.cshtml)

Redirect the user to the Index action

[HttpGet] public ActionResult Edit(int id) { using (var context = new MyAppContext()) { var emp = context.Employees .Include(x => x.Messages).Include(x => x.Department) .Where(x => x.EmployeeId == id).Single(); return View(emp); } } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, Employee employee ) { if ( ModelState.IsValid ) { if (employee.IpAddress == "127.0.0.1") { ModelState.AddModelError("", "Do not use 127.0.0.1"); return View(employee); } else { using (var context = new MyAppContext()) { var oldEmployee = Context.Employees.Find(id); UpdateModel(oldEmployee); context.SaveChanges(); return View("Details", employee); } } } else { return View(employee); } } } }

Use this action for GET requests (i.e.,

to retrieve an empty/current form)

Use this action when the user POSTs

back a form (i.e., saves changes)

Check for the token created by

@Html. AntiForgeryToken()

Were there any validation errors

when creating the Employee object?

Use HTML Get/Post parameters to

construct an Employee object

Add a custom validation error for

@Html.ValidationSummary()

Return the Details strongly typed

view (Details.cshtml)

Use HTML Get/Post parameters to

update the Employee object