Thursday, 19 December 2013

Repository Concept without strongly typed in MVC 4.0 using Razor View Engine

Repository Concept without  strongly typed in MVC 4.0 using Razor View Engine

-- Execute this query for Sql Server
create database ForMvc
use ForMvc

create table Employee (SrNo int identity(1,1),Name nvarchar(50),Gender nvarchar(50),Dob DateTime ,EmailId nvarchar(50) primary key,Password nvarchar(50),Qulification nvarchar(50),Address nvarchar(50),MobileNo nvarchar(50)not null,EmpImage nvarchar(max))


First we create class which name is Employer.cs……………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using this  namespace................
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MvcApplicationRepositoryPart2.Models
{
[Table("Employee")]
public class Employer
{
// Field SrNo is   in table with identity column
[Key]
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
public int SrNo { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public DateTime Dob { get; set; }
// Field EmailId is   in table with primary key ..........................
[DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None)]
public string EmailId { get; set; }
public string Password { get; set; }
public string Qulification{ get; set; }
public string Address { get; set; }
public string MobileNo { get; set; }
public string EmpImage{ get; set; }

}
}
Note: Because you are not using Model1.edmx in your MVCAppliction that’s why create class in Models which name is Employer.cs

Then we create class which name is database.cs……………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//this namespace for DbContext
using System.Data.Entity;
namespace MvcApplicationRepositoryPart2.Models
{
public class database : DbContext
{
// this DefaultConnection is name of ConnectionString which exist in Web.cnfig file
public database()
: base("DefaultConnection")
{
}
public DbSet<Employer> emp { get; set; }
}
}


Then after we create an Interface which name is IRepository.cs………………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MvcApplicationRepositoryPart2.Models;


namespace MvcApplicationRepositoryPart2.Models
{
public  interface IRepository
{

void Insert(string name,string gender,DateTime dob,string emailid,string password,string qulification,string address,string mobileno,string empimage);

Employer SeachByEmailId(string email);
}
}

Next step we create a class which name is Repository.cs which implement IRepository interface………………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcApplicationRepositoryPart2.Models
{
public class Repository : IRepository
{
database d = new database();
//code for save data in table
public void Insert(string name, string gender, DateTime dob, string emailid, string password, string qulification, string address, string mobileno, string empimage)
{

Employer emp = new Employer
{
Name = name,
Gender = gender,
Dob = dob,
EmailId = emailid,
Password = password,
Qulification = qulification,
Address = address,
MobileNo = mobileno,
EmpImage = empimage
};

d.emp.Add(emp);
d.SaveChanges();
}
// code for search details with emailid
public Employer SeachByEmailId(string emailid)
{
Employer  emp1 = (from m in d.emp where m.EmailId == emailid select m).FirstOrDefault();
return emp1;
}
}
}


Code for HomeController.cs……………………………………………..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationRepositoryPart2.Models;
using System.IO;

namespace MvcApplicationRepositoryPart2.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
IRepository i1;
public HomeController()
{
i1 = new Repository();
}
public ActionResult Registration()
{
return View();
}
[HttpPost]
//code for save data...................
public ActionResult Registration(FormCollection fc, HttpPostedFileBase file)
{
try
{
//In this code we  are working with file upload ,It must upload only jpgand jpeg format and size less than 10 kb
var allowedExtensions = new[] { ".jpg", ".jpge" };
var extension = Path.GetExtension(file.FileName);

if (file.ContentLength <= 10885)
{
if (allowedExtensions.Contains(extension))
{
//string name = Guid.NewGuid().ToString().Replace("-", "");
var filename = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/UserImages/"), filename);
file.SaveAs(path);
i1.Insert(fc["FullName"].ToString(), fc["Gender"].ToString(), Convert.ToDateTime(fc["dob"]), fc["email"].ToString(), fc["pass"].ToString(), fc["Quli"].ToString(), fc["Address"].ToString(), fc["mb"].ToString(), file.FileName);
ViewData["msg"] = 1;

}
else
{
ViewData["msg"] = 2;// "Please upload only  jpg and jpge file for image";
}
}
else
{
ViewData["msg"] = 3;//"upload only 10 kb ";
}
}
catch (Exception ex)
{
ViewData["error"] = ex.Message;
}
return View();
}
public ActionResult search()
{

return View();

}
// for  search with emailid......................................

[HttpPost]
public ActionResult search(string emailid)
{
var v=  i1.SeachByEmailId(emailid);
return View(v);

}
}
}
Code for Registration.cshtml……………………………………………..
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Registration</title>
</head>
<body>

<form action="" method="post" enctype="multipart/form-data" >
<div class="signup" id="new">
@using(Html.BeginForm()){
<table width="100%" border="5px">
<tr><td colspan="2"><marquee>My Registration Page</marquee></td></tr>

<tr><td>Name:</td><td>@Html.TextBox("FullName")</td></tr>

@*How to create Radio Button in Mvc*@

<tr><td>Gender:</td><td>
@Html.RadioButton("Gender", "Male", true) Male
@Html.RadioButton("Gender","Female") FeMale
</td></tr>
<tr><td>DateOfBirth:</td><td>@Html.TextBox("dob")</td></tr>
<tr><td>Emailid :</td><td>@Html.TextBox("email")</td></tr>

@*Code for create TextBox with Password Field in Mvc*@
<tr><td>Password :</td><td>@Html.Password("Pass")</td></tr>

@*How to create DropDownList in Mvc *@

<tr><td>Qulification</td>
<td>@Html.DropDownList("Quli",new []
{
new SelectListItem { Text="--Select--", Value="0" },
new SelectListItem { Text="HighSchool", Value="HighSchool"},
new SelectListItem { Text="Intermidiate", Value="Intermidiate" },
new SelectListItem { Text="Graduation", Value="Graduation"},
new SelectListItem { Text="Post Graduation", Value="Post Graduation"}
}
)

</td></tr>
<tr><td>Address:</td><td>@Html.TextArea("Address",new { row="6",cols="19"}) </td></tr>

<tr><td>Mobile No</td><td>@Html.TextBox("mb")</td></tr>

<tr><td>Profile Image:</td><td><input name="file" type="file" /></td></tr>
@*  <tr><td>Picture:</td><td><img src= "@Url.Content(Model.PicPath)" alt="Image"/</td></tr>*@


<tr><td colspan="2"><input type="submit" name="submit" id="submit" value="Submit" /> @Html.ActionLink("Search","Search")</td></tr>

<tr><td colspan="2">
<div style="color:red;">@ViewData["error"]
</div></td></tr>
<tr><td colspan="2">
@if (ViewData["msg"] != null)
{
if (ViewData["msg"].ToString() == "1")
{
<div style="color:Green;">Registration Successful.</div>
}
else if (ViewData["msg"].ToString() == "2")
{
<div style="color:Orange;">Please upload only  jpg and jpge file for image.</div>
}
else
{
<div style="color:Red;">upload only 10 kb.</div>
}
}

</td></tr>

</table>
}
</div>
</form>
</body>
</html>

Result:




Code for Search.cshtml……………………………………………..

@model MvcApplicationRepositoryPart2.Models.Employer

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>search</title>
</head>
<body>
<div>

@using (Html.BeginForm()) {
<table>
<tr><td colspan="2"></td></tr>
<tr><td>Search with  EmailId</td><td>@Html.TextBox("emailid")</td></tr>
<tr><td>  <input type="submit" value="Search" /></td><td></td></tr>
<tr><td>@Html.ActionLink("Edit", "Edit")</td><td>@Html.ActionLink("Back to List", "Registration")</td></tr>
</table>
}
</div>
@if(Model!=null)
{  <fieldset>
<legend>Employer</legend>
<table width="100%">
<tr><td>Name:</td><td>@Html.DisplayFor(model => model.Name)</td></tr>
<tr><td>Gender:</td><td>@Html.DisplayFor(model => model.Gender)</td></tr>
<tr><td>EmailId:</td><td>@Html.DisplayFor(model =>model.EmailId)</td></tr>
<tr><td>Dob</td><td>@Html.DisplayFor(model => model.Dob)</td></tr>
<tr><td>@Html.DisplayNameFor(model => model.Qulification)</td><td>@Html.DisplayFor(model => model.Qulification)</td></tr>
<tr><td>MobileNo</td><td>@Html.DisplayFor(model => model.MobileNo)</td></tr>
<tr><td>User Image</td><td><img src="../../UserImages/@Model.EmpImage"  height="100px" width="100px"/></td></tr>
</table>
</fieldset>

}
</body>
</html>

Result:




0 comments:

Post a Comment