Entity Framework / Validation Model: (Key) Databasegeneratedoption - Identity) )
Entity Framework / Validation Model: (Key) Databasegeneratedoption - Identity) )
Entity Framework / Validation Model: (Key) Databasegeneratedoption - Identity) )
namespace MyApp.Model {
public class Employee
{ Database primary key
[Key]
[DatabaseGeneratedAttribute(
Key is automatically generated by
DatabaseGeneratedOption.Identity)]
incrementing a counter
public int EmployeeId { get; set; }
[Required]
public string FamilyName { get; set; } Property value must be formatted as
an email address
[EmailAddress]
public string Email { get; set; }
Property value must match the
supplied regular expression
[RegularExpression("\\d+\\.\\d+\\.\\d+\\.\\d+",
ErrorMessage = "Invalid IP address format")]
public string IpAddress { get; set; } Declare navigation properties to be
virtual if you want lazy loading
public virtual Department Department { get; set; }
This list contains all Messages with an
[InverseProperty("Author")] Author property that is this Employee
public virtual ICollection<Message> Messages { get; set; }
Razor View
@using MyApp.Models
Declare a typed View
@model Employee
@{
Use a master layout / template
Layout = "~/Views/_Base.cshtml";
ViewBag.Title = "Employee Information";
} Set a property of the ViewBag
dynamic object
<h2>Employee Information</h2>
<p>These are the details of @Model.FullName:</p>
@using (Html.BeginForm())
Write out a value
{
@Html.AntiForgeryToken()
@Html.ValidationSummary() Create a HTML <form> tag
<div>
First Name: @Html.TextBoxFor(x => x.FirstName) Generate a token to prevent Cross-
</div> Site-Forgery-Requests (CSFRs)
<div>
Family Name: @Html.TextBoxFor(x => x.FamilyName)
</div> Show any validation errors
<div>
Birth Month: @Html.TextBoxFor(x => x.BirthMonth)
Create Text-boxes for Properties of
</div> the Model
<div>
IP Address: @Html.TextBoxFor(x => x.IpAddress)
</div>
<div>
Full Time:
@Html.DropDownListFor( Create a Drop-down list for IsFullTime
x => x.IsFullTime, property of the model
new [] {
new SelectListItem {
Text = "Full Time",
Value = "true",
Selected = Model.IsFullTime
},
new SelectListItem {
Text = "Part Time",
Value = "false",
Selected = !Model.IsFullTime
}
})
</div>
<input type="submit" value="Save Changes" />
} Razor comments