শুক্রবার, ১৩ আগস্ট, ২০১০

***ASP.NET MVC 2: Model Validation and Client side validation.

***ASP.NET MVC 2: Model Validation and Client side validation


# Persisting to a Database










# Enabling Validation in model with ASP.NET MVC 2 and DataAnnotations

[MetadataType (typeof (EmployeeInfo))]

public class EmployeeInfo

{



private String empID;

[DisplayName("Employee ID")]

[StringLength(20,ErrorMessage="Can't be longer than 20")]

[Required (ErrorMessage="Must enter ID")]



public string EmpID

{

get { return empID; }

set { empID = value; }

}

private string empName;

[StringLength (50,ErrorMessage="Can't Be Longer than 50 char")]

[DisplayName("Employee Name")]

[Required (ErrorMessage="Must Enter")]



public string EmpName

{

get { return empName; }

set { empName = value; }

}

private DateTime date;

[DisplayName("Joing Date")]



public DateTime Date

{

get { return date; }

set { date = value; }

}

private decimal salary;

[DisplayName ("Salary")]

[Range (1000.00,100000.00,ErrorMessage="Number Must Entered Between 1000.00 To 100000.00")]

public decimal Salary

{

get { return salary; }

set { salary = value; }

}

}


# Create the EmployeeInfoController page and in Controller Page take the following steps-

• Right click on Controller folder and click on Controller to create a controller














• After that the follow dialog box appeared And then click on Add button.











public ActionResult Create()

{

return View();

}

HRMEntities entity = new HRMEntities();

//

// POST: /EmployeeInfo/Create



[HttpPost]

public ActionResult Create(FormCollection collection)

{

try

{

// TODO: Add insert logic here

EmployeeInformation employee = new EmployeeInformation();

employee.EmployeeId = collection[0].ToString();

employee.EmployeeName = collection[1].ToString();

employee.Salary = Convert.ToDecimal(collection[3].ToString());

employee.JoiningDate = Convert.ToDateTime(collection[2].ToString());

entity.AddToEmployeeInformations(employee);

return RedirectToAction("Create");

}

catch

{

return View();

}

}


# Create the view page and take the following steps

<h2>Create </h2>
<% Html.EnableClientValidation(); %>
<%: Html.ValidationSummary(true) %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields </legend>

<div class="editor-label">
<%: Html.LabelFor(model => model.EmpID) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.EmpID) %>
<%: Html.ValidationMessageFor(model => model.EmpID) %>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.EmpName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.EmpName) %>
<%: Html.ValidationMessageFor(model => model.EmpName) %>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Date) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Date) %>
<%: Html.ValidationMessageFor(model => model.Date) %>
</div>

<div class="editor-label">
<%: Html.LabelFor(model => model.Salary) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Salary) %>
<%: Html.ValidationMessageFor(model => model.Salary) %>
</div>


<p>
<input type="submit" value="Create" />
</p>
</fieldset>

<% } %>


• Click right button on Controller Create Method and add a view


















• Click on Add View then will create the following page and the design view code is as follows-


# After that run and test











• After that add the following two link above the tag-
<h2>Create </h2>

<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"> </script>
<script src="../../Scripts/MicrosoftMvcValidation.js" type="text/javascript"> </script>

কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন