Web
Analytics
strongly typed html helpers in asp.net mvc razor | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

Strongly typed html helpers are introduced in MVC 2. In this kind of helpers we can generate html markup or view using model properties and lambda expression. These methods use a “Html.HelperNameFor()” naming convention. for example we have created model property like public string username{get;set;} We can create text box for username property in following way

@Html.TextBoxFor(m=>m.username)

HTML Element Helpers:

  • Html.TextBoxFor()
  • Html.TextAreaFor()
  • Html.DropDownListFor()
  • Html.CheckboxFor()
  • Html.RadioButtonFor()
  • Html.ListBoxFor()
  • Html.PasswordFor()
  • Html.HiddenFor()
  • Html.LabelFor()

Lets take an example where we are creating a registration form and going to show filled values on the same view.

Step1. Open VisualStudio=>Create New Project->Select Web in Visual C#->Asp.net web application->Select Empty and check MVC Checkbox

Step 2: Create a model “Employee.cs”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace StateManagement.Models
{
public class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public string Address { get; set; }
public string state { get; set; }
public string[] hobbies { get; set; }
}
}

Step3: Create New Controller “Student”

Step 4: CReate new action strongly.

public ActionResult Strongly()
{

return View();
}

Step 5: Add View and pass model reference into newly created view.

@model StateManagement.Models.Employee
@{
Layout = null;
}


@helper getall(string[] args)
{
    @foreach (var item in args) {
  • @item
  • }
} Strongly
@using (Html.BeginForm()) { @*@Html.EditorForModel()*@ @Html.TextBoxFor(m => m.EmployeeId)
@Html.TextBoxFor(m => m.EmployeeName)
@Html.TextBoxFor(m => m.Address)
@Html.DropDownListFor(m => m.state, new SelectList(new[] { "Raj", "UP", "MP" }))
@Html.ListBoxFor(m => m.hobbies, new MultiSelectList(new[] { "Reading", "Writing", "Internet" }))
}
@{ if (@ViewBag.EmployeeId != null) {

EmployeeId

@ViewBag.EmployeeId

EmployeeName

@ViewBag.EmployeeName

Address

@ViewBag.Address

Hobbies

@getall((string[])ViewBag.hobbies)

State

@ViewBag.State
} }

In above code I created a simple registration form where I have taken dropdown and listbox also.I listbox we are taking hobbies from user and user can select multiple properties.

if block(if (@ViewBag.EmployeeId != null)) will work only after data submitted by end user and it is showing the filled values.

Step 6 : Now Write following code into student controller.

[HttpPost]
public ActionResult Strongly(Employee empobj)
{

ViewBag.EmployeeId = empobj.EmployeeId;
ViewBag.EmployeeName = empobj.EmployeeName;
ViewBag.Address = empobj.Address;
ViewBag.state = empobj.state;
ViewBag.hobbies =(string[])empobj.hobbies;
return View();
}

Now run and execute above code .

Output

output

ASP.NET Core 2.0 Online Training

ASP.NET Core 2.0 Online Training