Controller Page[StudentController.cs]
===============================================
StudentRepository studentRepositoryObj = new StudentRepository(); //LINQ to SQL Classes
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection formValus)
{
StudentInfo studentObj = new StudentInfo();
studentObj.Name = Request.Form["Name"].Trim();
if (studentObj.Name.Length == 0)
{
ModelState.AddModelError("Name", "Name is required.");
}
studentObj.Age = Convert.ToInt16(Request.Form["Age"]);
if (studentObj.Age < 69 || studentObj.Age > 420)
{
ModelState.AddModelError("Age", "Age between 69 to 420.");
}
studentObj.Type = Request.Form["Type"];
if (studentObj.Type.Length == 0)
{
ModelState.AddModelError("Type", "Type Not Selected.");
}
string status = Request.Form["Status"];
bool state;
if (bool.TryParse(status, out state))
{
studentObj.Status = state;
}
string isActive = Request.Form["IsActive"];
if (bool.TryParse(isActive, out state))
{
studentObj.IsActive = state;
}
else
{
ModelState.AddModelError("IsActive", "Tick on Activity");
}
if (!ModelState.IsValid)
{
return View("Create", studentObj);
}
studentRepositoryObj.Add(studentObj);
studentRepositoryObj.Save();
return RedirectToAction("Index");
}
View Page [Create.cs]
===============================================
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary("An Error Occured") %><%--Display All Controller Message if Field is Balnk--%>
<p>
<%= Html.Label("Name") %>:
<%= Html.TextBox("Name")%>
<%= Html.ValidationMessage("Name","*") %>
</p>
<p>
<%= Html.Label("Age") %>:
<%= Html.TextBox("Age") %>
<%= Html.ValidationMessage("Age", "*") %>
</p>
<%= Html.Label("Type") %>:
<%= Html.DropDownList("Type", new SelectList(type), "Select")%>
<%=Html.ValidationMessage("Type", "Select Type") %>
<p>
</p>
<%= Html.Label("Status") %>:
<%= Html.CheckBox("Status")%>
</p>
<p>
Is Active:
<%= Html.RadioButton("IsActive","True")%>Yes
<%= Html.RadioButton("IsActive", "False")%>No
<%=Html.ValidationMessage("IsActive", "Activity not Selected") %>
</p>
<p>
<input type="submit" value="Create" />
</p>
<% } %>
No comments:
Post a Comment