TextBox=======================
<input type="text" name="firstname" id="firstname" />
@Html.TextBox("firstname")
<input type="text" name="firstname" id="firstname" value="John" />
@Html.TextBox("firstname", "John")
<input type="text" name="firstname" id="firstname" value="John" style="background-color:Red; color:White; font-weight:bold"/>
@Html.TextBox("firstname", "John", new {style="background-color:Red; color:White; font-weight:bold"})
@Html.TextBox("firstname", "John", new {style="background-color:Red; color:White; font-weight:bold", title="Please enter your first name"})
@Html.TextBox("firstname", "John", new { @class="redtextbox", @readonly ="true"}) <--prevent from reserved keyword
//to get a css link, drag and drop the css file
Password Type:
@Html.Password("Password")
Multiline Type
@Html.TextArea("Comments","", 5, 20, null) //name, content, col, row, style
Hidden Type
@Html.Hidden("Id")
Label===========================
@Html.Label("firstname","First Name")
DropDownList=====================
@Html.DropDownList("Departments", new List<SelectListItem>
{
new SelectListItem {Text="IT", Value="1", Selected=true},
new SelectListItem {Text="HR", Value="2"},
new SelectListItem {Text="Payroll", Value="3"},
}, "Select Department")
Radio ===================
.cshtml
@using (Html.BeginForm())
controller
[HttpGet]
{
foreach (var department in Model.Departments)
{
@Html.RadioButtonFor(m=>m.SelectedDepartment, department.Id) @department.Name
}
<br />
<br />
<input type="submit" value="submit" />
}
public ActionResult Index()
{
CompanyRadio companyRadio = new CompanyRadio();
return View(companyRadio);
}
[HttpPost]
public string Index(CompanyRadio companyRadio)
{
if(string.IsNullOrEmpty(companyRadio.SelectedDepartment))
{
return "You did not select any department";
}
else{
return "You Selected department with Id = " + companyRadio.SelectedDepartment;
}
}
CheckBox================ using template (look at listbox files)
.cshtml
@using (Html.BeginForm())
controller
[HttpGet]
{
@Html.EditorForModel()
<br />
<input type="submit" value="Submit" />
}
public ActionResult Index()
{
CityDB db = new CityDB();
return View(db.tblCities);
}
[HttpPost]
public string Index(IEnumerable<tblCity> cities)
{
if (cities.Count(x => x.IsSelected) == 0)
{
return "You have not selected any City";
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append("You selected -");
foreach (tblCity city in cities)
{
if (city.IsSelected)
{
sb.Append(city.name + ",");
}
}
sb.Remove(sb.ToString().LastIndexOf(","), 1);
return sb.ToString();
}
}
using template -
controller folder[ListBox] > EditorTemplates > class name[tblCity.cshtml]
ListBox ==============look at CityListBox
using a class
public class CitiesViewModel
{
public IEnumerable<SelectListItem> Cities { get; set; }
public IEnumerable<string> SelectedCities { get; set; }
}
.cshtml
@using (@Html.BeginForm())
controller
[HttpGet]
{
@Html.ListBoxFor(x => x.SelectedCities, Model.Cities, new { size = 6 })
<br />
<input type="submit" value="submit" />
}
public ActionResult Index()
{
CityDB db = new CityDB();
List<SelectListItem> listSelectListItem = new List<SelectListItem>();
foreach (tblCity city in db.tblCities)
{
SelectListItem selectListItem = new SelectListItem()
{
Text = city.name,
Value = city.id.ToString(),
Selected = city.IsSelected
};
listSelectListItem.Add(selectListItem);
}
CitiesViewModel citiesViewModel = new CitiesViewModel();
citiesViewModel.Cities = listSelectListItem;
return View(citiesViewModel);
}
[HttpPost]
public string Index(IEnumerable<string> selectedCities)
{
if(selectedCities == null)
{
return "You did not select any city";
}
else
{
StringBuilder sb = new StringBuilder();
sb.Append ("You selected - " + string.Join(", ", selectedCities));
return sb.ToString();
}
}