How to use Data
Annotations for Model Validation in Asp.Net MVC 5 using Linq without
dbml context
In this topic we
will discuss how to implement Data
Annotations for Model Validation and connectivity with mvc web from to database
sql server using linq here......
First step: We will add class which name is Employee in Model for Linq without dbml context
and Data Annotations for Model
Validation
We will create class which name EmpInfo
and implementing DataAnnotations validation
and Table and Column attributes because we are working linq without dbml context
Code for
EmpInfo.cs in Model Folder....................
using System;
using
System.Collections.Generic;
using
System.ComponentModel.DataAnnotations;
using System.Data.Linq.Mapping;
using System.Linq;
using System.Web;
namespace
ValidationByAshishSir.Models
{
[Table]
public class EmpInfo
{
[Required(ErrorMessage = "Please
Enter Name")]
[Column(IsPrimaryKey=true)]
public int Id { get; set; }
[Required(ErrorMessage = "Please Enter
Name")]
[StringLength(10, MinimumLength =
2, ErrorMessage = "your name must be 4 charters")]
[Column(Name="Name")]
public string EmpName { get; set; }
[Required(ErrorMessage = "Please Enter
Emailid")]
[EmailAddress(ErrorMessage = "your
emailid not format")]
[Column]
public string EmailId { get; set; }
[Required(ErrorMessage = "Please Enter
Password")]
[Column]
public string Password { get; set; }
[Required(ErrorMessage = "Please Enter
Confirm Password")]
[Compare("Password", ErrorMessage = "you pass and
cpass is not match")]
public string ConfirmPassword { get; set; }
[Required(ErrorMessage = "Please Enter
MobileNo")]
[RegularExpression(@"\d{10,11}")]
[Column(Name="MobileNo")]
public string MobileNo { get; set; }
[Required(ErrorMessage = "Please Enter
websiteaddress")]
[Url(ErrorMessage = "please enter
valid web address")]
[Column(Name="WebSite")]
public string WebAddress { get; set; }
[Required(ErrorMessage = "Please Enter
creadit card")]
[CreditCard(ErrorMessage = "please enter
valid credit card no")]
[Column]
public string CreditCard { get; set; }
[Required(ErrorMessage = "please enter
age")]
[Range(18, 60,
ErrorMessage = "your age b/w 18 to 60")]
[Column]
public int Age { get; set; }
}
}
Second step: We have to mention connection string in web.config file because we are
using linq without dbml context
<connectionStrings>
<add name="ashish" connectionString="Data
Source=ADMIN-PC;Initial Catalog=test;User ID=sa;Password=123" providerName="System.Data.SqlClient"/>
</connectionStrings>
Third step: we will add view with corresponding controller i.e. given below in
the snapshot
Code for
HomeController.cs …………………………….
using System;
using
System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using
ValidationByAshishSir.Models;
using
System.Configuration;
namespace
ValidationByAshishSir.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult
RegistrationPage()
{
return View();
}
DataContext dd = new DataContext(ConfigurationManager.ConnectionStrings["ashish"].ConnectionString);
[HttpPost]
public ActionResult RegistrationPage(EmpInfo emp)
{
Session["empid"] = emp.Id;
EmpInfo e1 = new EmpInfo
{
Id = emp.Id,
EmpName = emp.EmpName,
Age = emp.Age,
EmailId = emp.EmailId,
Password = emp.Password,
CreditCard = emp.CreditCard,
MobileNo = emp.MobileNo,
WebAddress = emp.WebAddress
};
dd.GetTable<EmpInfo>().InsertOnSubmit(e1);
dd.SubmitChanges();
ViewBag.msg = "saved";
return View();
}
}
}
How to add View using scaffolding concept i.e. given below snapshot please following this step……
Source
code for RegistrationPage.cshtml…………………………………….
@model ValidationByAshishSir.Models.EmpInfo
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>RegistrationPage</title>
</head>
<body>
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<link href="~/Content/Site.css" rel="stylesheet" />
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>EmpInfo</legend>
<div class="editor-label">
@Html.LabelFor(model
=> model.Id)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Id)
@Html.ValidationMessageFor(model
=> model.Id)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.EmpName)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.EmpName)
@Html.ValidationMessageFor(model
=> model.EmpName)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.EmailId)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.EmailId)
@Html.ValidationMessageFor(model
=> model.EmailId)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Password)
@Html.ValidationMessageFor(model
=> model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.ConfirmPassword)
@Html.ValidationMessageFor(model
=> model.ConfirmPassword)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.MobileNo)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.MobileNo)
@Html.ValidationMessageFor(model
=> model.MobileNo)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.WebAddress)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.WebAddress)
@Html.ValidationMessageFor(model
=> model.WebAddress)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.CreditCard)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.CreditCard)
@Html.ValidationMessageFor(model
=> model.CreditCard)
</div>
<div class="editor-label">
@Html.LabelFor(model
=> model.Age)
</div>
<div class="editor-field">
@Html.EditorFor(model
=> model.Age)
@Html.ValidationMessageFor(model
=> model.Age)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to
List",
"Index")
</div>
</body>
</html>
Result
0 comments:
Post a Comment