asp.net registration form with built in type html helpers | example
Example 3: In this example we will create a form and submit and show data
Action
public ActionResult BuiltInTypehelpers()
{
return View();
}
View
@{
ViewBag.Title = "BuiltInTypehelpers";
}
<style>
.myclass {
width: 400px;
border: 1px solid brown;
height: 35px;
border-radius: 5px;
margin-left: 50px;
}
.myclass1 {
margin-left: 100px;
}
</style>
<h2>BuiltInTypehelpers</h2>
@using (Html.BeginForm("SubmitData", "HtmlHelpersEx", FormMethod.Post))
{
<table>
<tr>
<td> @Html.Label("Enter Name") </td>
<!-- Add CSS class , required html attribute and Placeholder using htmlattributes -->
<td>@Html.TextBox("txt1", "", new { @class = "myclass", @placeholder = "Enter name", @required = "required" })</td>
</tr>
<tr>
<td> @Html.Label("Email") </td>
<!-- Add TextBox Type -->
<td>@Html.TextBox("txt2", "", new { @class = "myclass", @placeholder = "Enter name", @type = "email", @required = "required" })</td>
</tr>
<tr>
<td> @Html.Label("State") </td>
<td>
@Html.DropDownList("StateList", new List<SelectListItem> { new SelectListItem { Text = "Raj", Value = "1" }, new SelectListItem { Text = "UP", Value = "2" } }, "--Select State--", new { @class = "myclass", @required = "required" })
<!-- To Get Selected Text instead of value create hidden field-->
<input type="hidden" name="hidden1" id="hidden1" value="" />
</td>
</tr>
<tr>
<td> @Html.Label("State") </td>
<td>
@Html.RadioButton("gender", "male", new { @class = "myclass1" })@Html.Label("Male")
@Html.RadioButton("gender", "female")@Html.Label("Female")
</td>
</tr>
<tr>
<td> @Html.Label("hobbies") </td>
<td>
<label><input type="checkbox" name="name1" value="Dancing" /> Dancing </label><label> <input type="checkbox" name="name1" value="Reading" /> Reading </label><label> <input type="checkbox" name="name1" value="Internet" /> Internet </label>
</td>
</tr>
<tr>
<td> @Html.Label("Comments") </td>
<td>
@Html.TextArea("comments", new { @class = "myclass", @rows = "5", @col = "5" })
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="sub1" value="Save" class="btn btn-primary btn-lg" />
@*@Html.ActionLink("Go To Inline Helper", "InlineHTMLHelper" , "HtmlHelpersEx", new { id1 = "sdsdsds" }, null)*@
</td>
</tr>
</table>
}
<script src='@Url.Content("~/Scripts/jquery-1.10.2.min.js")'></script>
@*<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>*@
<script>
// Code to take text from selected item from dropdown
$(document).ready(function () {
$("#StateList").change(function () {
var ddtext = $("#StateList option:selected").text();
$("#hidden1").val(ddtext);
})
})
</script>
Custom Class(Employee2):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace testCode.CustomClasses
{
public class Employee2
{
public string Name;
public string Email;
public string StateList;
public string gender;
public string[] hobbies;
public string comments;
}
}
Action
[HttpPost]
// Get Data using FormCollection and CheckBox Data into name1 array
public ActionResult SubmitData(FormCollection form , string[] name1)
{
var Name1 = form["txt1"].ToString();
var Email1 = form["txt2"].ToString();
// To get Value of State
// var State = form["StateList"].ToString();
//To Get Selected Text of DropDown
var State = form["hidden1"].ToString();
var gender = form["gender"].ToString();
// Create new array to get checkboxes data and initilze with checked values
string[] arraydata = new string[name1.Length];
for(var i=0; i<name1.Length; i++)
{
if(name1[i]!= null)
{
arraydata[i] = name1[i].ToString();
}
}
var Comments = form["comments"].ToString();
//TempData["Name"] = Name;
//TempData["Email"] = Email;
// Initile Custom Class
Employee2 obj = new Employee2 { Name = Name1, Email = Email1 , hobbies = arraydata, gender = gender, StateList= State, comments = Comments};
//Store Employee Object Into TempData and Pass it
TempData["Data"] = obj;
return RedirectToAction("ShowData");
}
public ActionResult ShowData()
{
if (TempData["Data"] != null)
{
return View();
}
else
{
return RedirectToAction("BuiltInTypehelpers");
}
}
View(Show Data)
@using testCode.CustomClasses
@{
ViewBag.Title = "ShowData";
Employee2 obj2 = ((Employee2)TempData["Data"]);
}
<h2>ShowData</h2>
@*<h4>Name : @TempData["Name"]</h4>
<h4>Email : @TempData["Email"]</h4>*@
<h4>Name : @obj2.Name</h4>
<h4>Email : @obj2.Email</h4>
<h4>Hobbies</h4>
<ul>
@foreach(var data in obj2.hobbies)
{
<li>@data</li>
}
</ul>
<h4>Gender : @obj2.gender</h4>
<h4>State: @obj2.StateList</h4>
<h4>Comments: @obj2.comments</h4>