Hi all,
I am beginner for MVC so i want CRUD operation in asp.net with out entity fremwork and Razor give me simple example
Thanks
Rajveer Singh
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Chandan RajbharPosted Feb 13, 2017, 12:26 AM
Ayappan AlagesanPosted Feb 13, 2017, 12:17 AM
Chandan RajbharPosted Feb 11, 2017, 1:42 AM
Step 1-
First Create the database
Right Click on App_Data
Add database
click on database and create table
Step-2
Right Click on Model Add EnitityFrameWork
With table.
Step -3
Right Click On Controller
Add Controller
Create Database connation object
Right click on Model1.Context.tt
Under Model1.Context.cs
public UserEn()
: base("name=UserEn")
{
}
Copy or write
private UserEn _objDataBase = new UserEn();
Display Data in Grid
Step 1-
Right Click On view
using StudentMam.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace StudentMam.Controllers
{
public class HomeController : Controller
{
private UserEn _objDataBase = new UserEn();
//
// GET: /Home/
public ActionResult Index()
{
//// objDataBase is object of UserEn
//// Users is object
return View(_objDataBase.Users);
}
///create View for insert data in database
public ActionResult Create()
{
return View();
}
//logic for insert data into table
[HttpPost]
public ActionResult Create([Bind(Exclude="Id")] User obj)
{
_objDataBase.Users.Add(obj);
_objDataBase.SaveChanges();
return View();
}
}
}
Also write code this way
[HttpPost]
public ActionResult Create(User obj)
{
_objDataBase.Users.Add(obj);
_objDataBase.SaveChanges();
return View();
}
When we are add new Course
Create table
Declare Column name
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace StudentMam
{
public class Course
{
public int Id { get; set; }
public string CourseName { get; set; }
public string Duration { get; set; }
}
}
using StudentMam.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace StudentMam.Controllers
{
public class CourseController : Controller
{
private UserEn _ObjDataBase = new UserEn();
//
// GET: /Course/
//display all course
public ActionResult Course()
{
return View(_ObjDataBase.Courses);
}
//add course view
public ActionResult AddCourse()
{
return View();
}
//logic for add course
[HttpPost]
public ActionResult AddCourse(Course objCor)
{
_ObjDataBase.Courses.Add(objCor);
_ObjDataBase.SaveChanges();
return RedirectToAction("Course");
}
//diplay course for edit
public ActionResult Edit(int id = 0)
{
return View(_ObjDataBase.Courses.Find(id));
}
//update logic
[HttpPost]
public ActionResult Edit(Course obJEdit)
{
_ObjDataBase.Entry(obJEdit).State = EntityState.Modified;
_ObjDataBase.SaveChanges();
return RedirectToAction("Course");
}
//display particular corse details
public ActionResult Details(int id = 0)
{
return View(_ObjDataBase.Courses.Find(id));
}
//delete particular course
public ActionResult Delete(int id = 0)
{
return View(_ObjDataBase.Courses.Find(id));
}
//delete course logic
[HttpPost, ActionName("Delete")]
public ActionResult delete_Sure(int id = 0)
{
Course crs = _ObjDataBase.Courses.Find(id);
_ObjDataBase.Courses.Remove(crs);
_ObjDataBase.SaveChanges();
return RedirectToAction("Course");
}
}
}
Chandan RajbharPosted Feb 11, 2017, 1:39 AM
Shweta LodhaPosted Feb 12, 2016, 12:13 PM
Please have a look at below link:http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/crud-operation-in-Asp-Net-mvc-framework/