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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Shweta LodhaRank #31 · 39.8K reputationPosted Feb 12, 2016, 7:13 AM EST
Please have a look at below link:http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/crud-operation-in-Asp-Net-mvc-framework/
Chandan Rajbhar152 reputationPosted Feb 10, 2017, 8:39 PM EST
Chandan Rajbhar152 reputationPosted Feb 10, 2017, 8:42 PM EST
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");
}
}
}
Ayappan AlagesanRank #4,305 · 352 reputationPosted Feb 12, 2017, 7:17 PM EST
Chandan Rajbhar152 reputationPosted Feb 12, 2017, 7:26 PM EST