Saturday 7 December 2013

Creating an Entity Framework Data Model for an ASP.NET MVC 4.0 Application with strongly typed View

 Creating an Entity Framework Data Model for an ASP.NET MVC 4.0 Application with
strongly typed View
--Query for Sql Server 

create database TestDb
use TestDb
create table McaStudent(RollNo int identity(1,1) ,Name nvarchar(50),Gender nvarchar(50),EmailId nvarchar(50) primary key,Password nvarchar(50),MobileNo nvarchar(50)unique)

How to add Ado.Net Entity Data Models in MVC Application


Next step –Entity DataModel wizard


Which data connection should you application use to connection to the database


Choose your data Connection

Choose your database objects and setting 


This is your model.edmx file which contains the all tables related to project



How to add strongly typed View with controller in Mvc Application


Code for Registration.cshtml...............................

@model MvcwithEntityFrameWork.Models.McaStudent

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Registration</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>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
<legend>McaStudent</legend>

<div class="editor-label">
@Html.LabelFor(model => model.RollNo)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.RollNo)
@Html.ValidationMessageFor(model => model.RollNo)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>

<div class="editor-label">
@Html.LabelFor(model => model.Gender)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Gender)
@Html.ValidationMessageFor(model => model.Gender)
</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">
@*  for Password Field*@
@Html.PasswordFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</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>

<p>

<input type="submit" value="Create" />
</p>
<div style="color:red;"> @ViewData["error"]</div>
<div style="color:green;"> @ViewData["message"]</div>
</fieldset>
}

<div>
@Html.ActionLink("Back to List", "Registration")
</div>
</body>
</html>
Code for Controller.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//using namespace for database because your edmx file present in Models
using MvcwithEntityFrameWork.Models;

namespace MvcwithEntityFrameWork.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
//create object of TestDbEntity
TestDbEntities tt = new TestDbEntities();
public ActionResult Index()
{
return View();
}
public ActionResult Registration()
{
return View();
}
[HttpPost]
public ActionResult Registration(McaStudent mm)
{
try
{
tt.McaStudents.Add(mm);
tt.SaveChanges();
ViewData["message"] = "Save Successfully";

}
catch (Exception ex)
{
ViewData["error"] = ex.Message;
}
return View();
}
}
}


4 comments:

  1. what is the use of

    @{
    Layout = null;
    }

    ReplyDelete
  2. Hi Sir,
    Very Good Morning and wish U Happy Dushhara U And Ur Family......

    Please Give me idia Or code How to Biend DropDownList,RadioButtonList,CheckBoxList.... with EntityFrame in MVC 4 with Sqlserver Dynamicaly....

    Thanks & Regard
    Pankaj
    Your's Student.

    ReplyDelete