Skip to content

Instantly share code, notes, and snippets.

@himanshu717171
Created December 16, 2018 06:16
Show Gist options
  • Save himanshu717171/accfb8326cea6cf991e739840415d80d to your computer and use it in GitHub Desktop.
Save himanshu717171/accfb8326cea6cf991e739840415d80d to your computer and use it in GitHub Desktop.
CRUD Single Page Application Using ASP.Net MVC
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data;
using System.Data.SqlClient;
namespace MvcApplicationOmninet_CRUD_SinglePageApp_.Models
{
public class DataLayer:DbContext
{
public virtual IEnumerable<T> AddStudent<T>(Student S)
{
SqlParameter[] Para = new SqlParameter[]
{
new SqlParameter{ParameterName="@ProcID", Value=S.ProcID},
new SqlParameter{ParameterName="@ID", Value=S.ID},
new SqlParameter{ParameterName="@Name",Value=S.Name ??string.Empty},
new SqlParameter{ParameterName="@Email", Value=S.Email ??string.Empty},
new SqlParameter{ParameterName="@Mobile", Value=S.Mobile ??string.Empty},
new SqlParameter{ParameterName="@City", Value=S.City ??string.Empty},
new SqlParameter{ParameterName="@Country", Value=S.Country ??string.Empty},
};
string Query = "Proc_Student @ProcID,@ID,@Name,@Email,@Mobile,@City, @Country";
return this.Database.SqlQuery<T>(Query, Para);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationOmninet_CRUD_SinglePageApp_.Models;
namespace MvcApplicationOmninet_CRUD_SinglePageApp_.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Btn = "Create";
return View();
}
[HttpPost]
public ActionResult Index(Student model, string Btn)
{
if (Btn.ToLower() == "create") { model.ProcID = 1; ViewBag.Btn = "Create"; }
else if (Btn.ToLower() == "update") { model.ProcID = 3; ViewBag.Btn = "Update"; }
else if (Btn.ToLower() == "delete") { model.ProcID = 4; ViewBag.Btn = "Delete"; }
model.stdlist = new DataLayer().AddStudent<Student>(model).ToList();
return View();
}
public ActionResult StudentList()
{
Student model = new Student();
model.ProcID = 2;
model.stdlist = new DataLayer().AddStudent<Student>(model).ToList();
return View(model.stdlist);
}
public ActionResult Edit(int ID)
{
ViewBag.Btn = "Update";
Student model = new Student();
model.ProcID = 2;
model.ID = ID;
model.stdlist = new DataLayer().AddStudent<Student>(model).ToList();
return View("Index", model.stdlist.Single(x => x.ID == ID));
}
public ActionResult Delete(int ID)
{
ViewBag.Btn = "Delete";
Student model = new Student();
model.ProcID = 2;
model.ID = ID;
model.stdlist = new DataLayer().AddStudent<Student>(model).ToList();
return View("Index", model.stdlist.Single(x => x.ID == ID));
}
}
}
@model MvcApplicationOmninet_CRUD_SinglePageApp_.Models.Student
@{
ViewBag.Title = "Index";
}
<link href="~/Scripts/BootSrap/css/bootstrap.min.css" rel="stylesheet" />
<hr />
<div class="container">
<div class="row">
<div class="col-sm-12" style="text-align:center;color:red;font-family:Algerian;font-size:xx-large">
Student-Registration
</div>
</div>
</div>
<hr />
<div class="container">
@using(Html.BeginForm("Index","Home",FormMethod.Post))
{
@Html.HiddenFor(x => x.ID)
<table class="table table-bordered">
<tr>
<th>Name</th>
<td>@Html.TextBoxFor(x => x.Name,new {@class="form-control",@Resetkey="inputName" })</td>
</tr>
<tr>
<th>Email ID</th>
<td>@Html.TextBoxFor(x => x.Email,new {@class="form-control",@Resetkey="inputName" })</td>
</tr>
<tr>
<th>Mobile Number</th>
<td>@Html.TextBoxFor(x => x.Mobile,new {@class="form-control",@Resetkey="inputName" })</td>
</tr>
<tr>
<th>City</th>
<td>@Html.TextBoxFor(x => x.City,new {@class="form-control",@Resetkey="inputName" })</td>
</tr>
<tr>
<th>Country</th>
<td>@Html.TextBoxFor(x => x.Country,new {@class="form-control",@Resetkey="inputName" })</td>
</tr>
</table>
<div class="row">
<div class="col-sm-12">
<input type="submit" value="@ViewBag.Btn" name="Btn" class="btn btn-success" />
<input type="button" value="Reset" name="RBtn" class="btn btn-danger" />
</div>
</div>
}
</div>
@Html.Action("StudentList")
@section Scripts
{
<script>
$(document).ready(function ()
{
$('input[type="button"]').click(function ()
{
debugger;
$('[ResetKey*="input"]').each(function ()
{
debugger;
$(this).val("");
});
});
});
</script>
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplicationOmninet_CRUD_SinglePageApp_.Models
{
public class Student
{
public int ProcID { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
public string City { get; set; }
public string Country { get; set; }
public IList<Student> stdlist { get; set; }
}
}
CREATE TABLE [dbo].[Student](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL,
[Email] [varchar](50) NULL,
[Mobile] [varchar](50) NULL,
[City] [varchar](50) NULL,
[Country] [varchar](50) NULL,
PRIMARY KEY CLUSTERED
---------------------------------------------------------------------------------
USE [StudentInfo]
GO
/****** Object: StoredProcedure [dbo].[Proc_Student] Script Date: 16-Dec-18 11:32:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Proc_Student]
@ProcID int = 0,
@ID int = 0,
@Name Varchar(50) = Null,
@Email Varchar(50) = Null,
@Mobile Varchar(50) = Null,
@City Varchar(50) = Null,
@Country Varchar(50) = Null
As
Begin
if (@ProcID = 1)
Begin
insert into Student(Name,Email,Mobile,City,Country) Values(@Name,@Email,@Mobile,@City,@Country)
Select * from Student
End
if( @ProcID = 2)
Begin
Select * from Student
End
if(@ProcID = 3)
Begin
Update Student set Name = @Name, Email = @Email, Mobile = @Mobile, City = @City, Country = @Country where ID = @ID
Select * from Student
End
if (@ProcID = 4)
Begin
Delete from Student where ID = @ID
Select * from Student
End
End
@model IEnumerable<MvcApplicationOmninet_CRUD_SinglePageApp_.Models.Student>
@{
ViewBag.Title = "StudentList";
}
<hr />
<div class="col-sm-12" style="text-align:center;color:red;font-family:Algerian;font-size:xx-large">
Student-Details
</div>
<hr />
<div class="container">
<table class="table table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.Mobile)
</th>
<th>
@Html.DisplayNameFor(model => model.City)
</th>
<th>
@Html.DisplayNameFor(model => model.Country)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.Mobile)
</td>
<td>
@Html.DisplayFor(modelItem => item.City)
</td>
<td>
@Html.DisplayFor(modelItem => item.Country)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment