@using MvcSample.Web.Models @model User @{ ViewBag.Title = "Home Page"; string nullValue = null; ViewBag.Anon = new { Name = "FirstName LastName", Address = new { Street = "123 Fake St.", City = "Redmond", State = "WA", Country = "USA", }, }; } @section header { } @section footer { } @functions { public Task AsyncValueRetrieval() { return Task.FromResult("Hello World"); } public Task RenderHelloWorldPartial(User model) { // Imagine this method was a super complex method that was used as a helper method. return Html.RenderPartialAsync("HelloWorldPartial", model); } }

ASP.NET

ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.

Learn more »

Hello @Html.DisplayTextFor(User => User)! Happy @(Model?.Age) birthday.

This value was retrieved asynchronously: @(await AsyncValueRetrieval())

Partial Async: @await Html.PartialAsync("HelloWorldPartial", Model)

Render Partial Async (Custom model): @{ await RenderHelloWorldPartial(new User() { Name="Bob" }); }

Book a flight

Go to our cool travel reservation system.

@Html.ActionLink("Reserve Now", "Fly", "Flight", new { area = "Travel" }, new { @class = "btn btn-default" })

Get more libraries

NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.

Learn more »

Web Hosting

You can easily find a web hosting company that offers the right mix of features and price for your applications.

Learn more »

@Html.LabelForModel("ForModel") '@Html.DisplayNameForModel()' '@Html.NameForModel()' '@Html.ValueForModel()'
@Html.Label("Name") '@Html.DisplayName("Name")' '@Html.Name("Name")' '@Html.Value("Name")'
@Html.LabelFor(m => m.Address) '@Html.DisplayNameFor(m => m.Address)' '@Html.NameFor(m => m.Address)' '@Html.ValueFor(m => m.Address)'
@Html.Label("Anon.Address.Street") '@Html.DisplayName("Anon.Address.Street")' '@Html.Name("Anon.Address.Street")' '@Html.Value("Anon.Address.Street")'

Tags from View():

@await Component.InvokeAsync("Tags", 5, "View")

Tags from Content():

@await Component.InvokeAsync("Tags", 5, "Content")

Tags from Json():

@await Component.InvokeAsync("Tags", 5, "Json")

'@ViewBag.Title' should match page heading (still)

@using (Html.BeginForm()) { @Html.HiddenFor(m => m.Age)
@Html.Label("Name") @Html.TextBox("Name")
@Html.LabelFor(m => m.Address, htmlAttributes: new { @class="control-label col-md-2" }) @Html.TextBoxFor(m => m.Address, htmlAttributes: new { @class = "form-control" })
@Html.Label("Alive") @Html.CheckBox("Alive") or @Html.RadioButton("Alive", value: false) False @Html.RadioButton("Alive", value: true) True
@Html.LabelFor(m => m.Dependent.Alive) @Html.CheckBoxFor(m => m.Dependent.Alive) or @Html.RadioButtonFor(m => m.Dependent.Alive, value: false) False @Html.RadioButtonFor(m => m.Dependent.Alive, value: true) True
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class="control-label col-md-2" }) @Html.PasswordFor(m => m.Password, htmlAttributes: new { @class = "form-control" })
} @{ Html.BeginForm(method: FormMethod.Post, htmlAttributes: new { someAttribute = "some value", }); } @Html.Hidden("Anon.Id")
@Html.Label("Anon.Name") @Html.TextBox("Anon.Name")
@Html.Label("Anon.Address.Street") @Html.TextBox("Anon.Address.Street", (object)ViewBag.Anon.Address.Street)
@Html.Label("Anon.Address.City") @Html.TextBox("Anon.Address.City", value: null, format: "{0} (3)")
@Html.Label("Anon.Address.State") @Html.TextBox("Anon.Address.State", value: null, htmlAttributes: new { @class = "form-control" })
@Html.Label("Anon.Address.Country") @Html.TextBox("Anon.Address.Country", value: null, format: "{0} (4)", htmlAttributes: new { @class = "form-control" })
@Html.Label("Password") @Html.Password("Password", "some string")
@{ Html.EndForm(); }
@using (Html.BeginForm(controllerName: "Home", actionName: "Edit", method: FormMethod.Post)) {
@Html.Label("Name", "Display / Edit Name") '@Html.Display("Name")' @Html.Editor("Name")
@Html.Label("Dependent.Name", "Display / Edit Dependent.Name") '@Html.Display("Dependent.Name")' @Html.Editor("Dependent.Name")
@Html.Label("Alive", "Display / Edit Alive") '@Html.Display("Alive")' @Html.Editor("Alive")
@Html.Label("Dependent.Alive", "Display / Edit Dependent.Alive") '@Html.Display("Dependent.Alive")' @Html.Editor("Dependent.Alive")
@Html.Label("Age", "Display / Edit Age") '@Html.Display("Age")' @Html.Editor("Age")
@Html.Label("GPA", "Display / Edit GPA") '@Html.Display("GPA")' @Html.Editor("GPA")
}
@using (Html.BeginForm(controllerName: "Home", actionName: "Edit", method: FormMethod.Post)) {
@Html.LabelFor(model => model.Name, "DisplayFor / EditFor Name") '@Html.DisplayFor(model => model.Name)' @Html.EditorFor(model => model.Name)
@Html.LabelFor(model => model.Dependent.Name, "DisplayFor / EditFor Dependent.Name") '@Html.DisplayFor(model => model.Dependent.Name)' @Html.EditorFor(model => model.Dependent.Name)
@Html.LabelFor(model => model.Alive, "DisplayFor / EditFor Alive") '@Html.DisplayFor(model => model.Alive)' @Html.EditorFor(model => model.Alive)
@Html.LabelFor(model => model.Dependent.Alive, "DisplayFor / EditFor Dependent.Alive") '@Html.DisplayFor(model => model.Dependent.Alive)' @Html.EditorFor(model => model.Dependent.Alive)
@Html.LabelFor(model => model.Age, "DisplayFor / EditFor Age") '@Html.DisplayFor(model => model.Age)' @Html.EditorFor(model => model.Age)
@Html.LabelFor(model => model.GPA, "DisplayFor / EditFor GPA") '@Html.DisplayFor(model => model.GPA)' @Html.EditorFor(model => model.GPA)
}
@Html.DisplayForModel()
@Html.EditorForModel()